<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>03_webdriver</title>
<link rel="stylesheet" type="text/css" href="css/mystyle.css"/>
</head>
<body>


<div class="body">
            
  <div class="section" id="selenium-webdriver">
<h1>Selenium WebDriver
<a class="headerlink" href="#selenium-webdriver" title="Permalink to this headline">¶</a>
<span class="subTitle"><a href="http://docs.seleniumhq.org/docs/03_webdriver.jsp">online</a></span>
</h1>
<p id="chapter03-reference"><em>NOTE: We’re currently working on documenting these sections.
We believe the information here is accurate, however be aware we are also still working on this
chapter.  Additional information will be provided as we go which should make this chapter more
solid.</em></p>
<p>注意：我们正在编写这些部分的文档。 我们相信这里的信息是准确的，但是请注意，我们还在继续这一章。 我们将会提供更多的信息，这将使本章更加扎实。</p>


<div class="section" id="introducing-webdriver">
<h2>Introducing WebDriver<a class="headerlink" href="#introducing-webdriver" title="Permalink to this headline">¶</a></h2>
<p>The primary new feature in Selenium 2.0 is the integration of the WebDriver API.
WebDriver is designed to provide a simpler, more concise programming interface in addition to
addressing some limitations in the Selenium-RC API.  Selenium-WebDriver was developed
to better support dynamic web pages where elements of a page may change without the page
itself being reloaded.  WebDriver’s goal is to supply a well-designed object-oriented
API that provides improved support for modern advanced web-app testing problems.</p>
<p>Selenium 2.0中的主要新功能是WebDriver API的集成。  WebDriver除了解决Selenium-RC API中的一些限制外，还提供了一个更简单，更简洁的编程接口。 开发Selenium-WebDriver是为了更好地支持动态网页，页面元素可能会改变，页面本身不会被重新加载。  WebDriver的目标是提供一个设计良好的面向对象API，为现代高级Web应用程序测试问题提供更好的支持。</p>

</div>
<div class="section" id="how-does-webdriver-drive-the-browser-compared-to-selenium-rc">
<h2>How Does WebDriver ‘Drive’ the Browser Compared to Selenium-RC?<a class="headerlink" href="#how-does-webdriver-drive-the-browser-compared-to-selenium-rc" title="Permalink to this headline">¶</a></h2>
<p>Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.
How these direct calls are made, and the features they support depends on the browser you are using.
Information on each ‘browser driver’ is provided later in this chapter.</p>
<p>Selenium-WebDriver使用每个浏览器对自动化的原生支持直接调用浏览器。 如何进行这些直接调用，以及它们支持的功能取决于您正在使用的浏览器。 有关每个“浏览器驱动程序”的信息将在本章后面提供。</p>

<p>For those familiar with Selenium-RC, this is quite different from what you are used to.  Selenium-RC
worked the same way for each supported browser.  It ‘injected’ javascript functions into the browser
when the browser was loaded and then used its javascript to drive the AUT within the browser.
WebDriver does not use this technique.  Again, it drives the browser directly using the browser’s
built in support for automation.</p>
<p>对于那些熟悉Selenium-RC的人来说，这与你习惯的很不一样。  Selenium-RC对每个支持的浏览器都采用相同的方式。 当浏览器被加载后，它将JavaScript函数“注入”到浏览器中，然后使用其javascript在浏览器中驱动AUT。  WebDriver不使用这种技术。 同样，它使用内置的支持自动化的浏览器直接驱动浏览器。</p>

</div>
<div class="section" id="webdriver-and-the-selenium-server">
<h2>WebDriver and the Selenium-Server<a class="headerlink" href="#webdriver-and-the-selenium-server" title="Permalink to this headline">¶</a></h2>
<p>You may, or may not, need the Selenium Server, depending on how you intend to use Selenium-WebDriver.
If your browser and tests will all run on the same machine, and your tests only use the WebDriver API,
then you do not need to run the Selenium-Server; WebDriver will run the browser directly.</p>
<p>您可能或不需要Selenium服务器，具体取决于您打算如何使用Selenium-WebDriver。 如果您的浏览器和测试将全部在同一台机器上运行，并且您的测试只使用WebDriver API，那么您不需要运行Selenium-Server;  WebDriver将直接运行浏览器。</p>

<p>There are some reasons though to use the Selenium-Server with Selenium-WebDriver. <span class="subA">Selenium-WebDriver使用Selenium-Server有一些原因。</span></p>


<blockquote>
<div><ul class="simple">
<li>You are using Selenium-Grid to distribute your tests over multiple machines or virtual machines (VMs).
<span class="subA">您正在使用Selenium-Grid将测试分布到多台机器或虚拟机（VM）上。</span>
</li>
<li>You want to connect to a remote machine that has a particular browser version that is not on
your current machine.
<span class="subA">您要连接到具有特定浏览器版本的远程机器，该机器不在当前机器上。</span>
</li>
<li>You are not using the Java bindings (i.e. Python, C#, or Ruby) and would like to use <a class="reference internal" href="#htmlunit-driver">HtmlUnit Driver</a>
<span class="subA">您没有使用Java绑定（即Python，C＃或Ruby），并希望使用HtmlUnit Driver</span>
</li>
</ul>
</div></blockquote>
</div>
<div class="section" id="setting-up-a-selenium-webdriver-project">
<span id="setting-up-webdriver-project"></span><h2>Setting Up a Selenium-WebDriver Project<a class="headerlink" href="#setting-up-a-selenium-webdriver-project" title="Permalink to this headline">¶</a></h2>
<p>To install Selenium means to set up a project in a development so you can write a program using
Selenium.  How you do this depends on your programming language and your development environment.</p>
<p>安装Selenium意味着在一个开发中设置一个项目，这样你就可以使用Selenium编写一个程序。 你如何做到这一点取决于你的编程语言和你的开发环境。</p>


<div class="section" id="java">
<h3>Java<a class="headerlink" href="#java" title="Permalink to this headline">¶</a></h3>
<p>The easiest way to set up a Selenium 2.0 Java project is to use Maven.  Maven will download the
java bindings (the Selenium 2.0 java client library) and all its dependencies, and will create the
project for you, using a maven pom.xml (project configuration) file.  Once you’ve done this, you
can import the maven project into your preferred IDE, IntelliJ IDEA or Eclipse.</p>
<p>设置Selenium 2.0 Java项目的最简单方法是使用Maven。  Maven将下载java绑定（Selenium 2.0 java客户端库）及其所有依赖项，并使用maven pom.xml（项目配置）文件为您创建项目。 一旦你完成了这个，你可以将maven项目导入到你的首选IDE，IntelliJ IDEA或Eclipse中。</p>

<p>First, create a folder to contain your Selenium project files.  Then, to use Maven, you need a
pom.xml file.  This can be created with a text editor.  We won’t teach the
details of pom.xml files or for using Maven since there are already excellent references on this.
Your pom.xml file will look something like this.  Create this file in the folder you created for
your project.</p>
<p>首先，创建一个文件夹来包含你的Selenium项目文件。 然后，要使用Maven，你需要一个pom.xml文件。 这可以使用文本编辑器创建。 我们不会讲授pom.xml文件的细节或者使用Maven，因为已经有了很好的参考。 你的pom.xml文件看起来像这样。 在为您的项目创建的文件夹中创建此文件。</p>


<div class="highlight-xml"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="nt">&lt;project</span> <span class="na">xmlns=</span><span class="s">"http://maven.apache.org/POM/4.0.0"</span>
                 <span class="na">xmlns:xsi=</span><span class="s">"http://www.w3.org/2001/XMLSchema-instance"</span>
                 <span class="na">xsi:schemaLocation=</span><span class="s">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span><span class="nt">&gt;</span>
        <span class="nt">&lt;modelVersion&gt;</span>4.0.0<span class="nt">&lt;/modelVersion&gt;</span>
        <span class="nt">&lt;groupId&gt;</span>MySel20Proj<span class="nt">&lt;/groupId&gt;</span>
        <span class="nt">&lt;artifactId&gt;</span>MySel20Proj<span class="nt">&lt;/artifactId&gt;</span>
        <span class="nt">&lt;version&gt;</span>1.0<span class="nt">&lt;/version&gt;</span>
        <span class="nt">&lt;dependencies&gt;</span>
            <span class="nt">&lt;dependency&gt;</span>
                <span class="nt">&lt;groupId&gt;</span>org.seleniumhq.selenium<span class="nt">&lt;/groupId&gt;</span>
                <span class="nt">&lt;artifactId&gt;</span>selenium-server<span class="nt">&lt;/artifactId&gt;</span>
                <span class="nt">&lt;version&gt;</span>3.0.1<span class="nt">&lt;/version&gt;</span>
            <span class="nt">&lt;/dependency&gt;</span>
        <span class="nt">&lt;/dependencies&gt;</span>
<span class="nt">&lt;/project&gt;</span>
</pre></div>
</div>


<p>Be sure you specify the most current version.  At the time of writing, the version listed above was
the most current, however there were frequent releases immediately after the release of Selenium 2.0.
Check the <a class="reference external" href="http://seleniumhq.org/download/maven.html">Maven download page</a> for the current release and edit the above dependency accordingly.</p>
<p>确保你指定了最新的版本。 在撰写本文时，上面列出的版本是最新的版本，但是在发布Selenium 2.0后立即出现了频繁的版本。 检查当前版本的Maven下载页面，并相应地编辑上面的依赖关系。</p>


<p>Now, from a command-line, CD into the project directory and run maven as follows.</p>
<p>现在，从一个命令行，CD进入项目目录并运行maven，如下所示。</p>


<div class="highlight-bash"><div class="highlight"><pre><span></span>mvn clean install
</pre></div>
</div>
<p>This will download Selenium and all its dependencies and will add them to the project.</p>
<p>这将下载Selenium及其所有依赖项，并将其添加到项目中。</p>


<p>Finally, import the project into your preferred development environment.  For those not familiar
with this, we’ve provided an appendix which shows this.</p>
<p>最后，将项目导入到首选的开发环境中。 对于那些不熟悉的人，我们提供了一个附录来说明这一点。</p>


<p><a class="reference internal" href="appendix_installing_java_driver_Sel20_via_maven.jsp#importing-maven-into-intellij-reference"><span class="std std-ref">Importing a maven project into IntelliJ IDEA</span></a>.
<a class="reference internal" href="appendix_installing_java_driver_Sel20_via_maven.jsp#importing-maven-into-eclipse-reference"><span class="std std-ref">Importing a maven project into Eclipse</span></a>.</p>
</div>
<div class="section" id="c">
<h3>C#<a class="headerlink" href="#c" title="Permalink to this headline">¶</a></h3>
<p>As of Selenium 2.2.0, the C# bindings are distributed as a set of signed dlls along with other
dependency dlls. Prior to 2.2.0, all Selenium dll’s were unsigned.
To include Selenium in your project, simply download the latest
selenium-dotnet zip file from <a class="reference external" href="http://selenium-release.storage.googleapis.com/index.html">http://selenium-release.storage.googleapis.com/index.html</a>.
If you are using Windows Vista or above, you should unblock the zip file before
unzipping it: Right click on the zip file, click “Properties”, click “Unblock”
and click “OK”.</p>
<p>Unzip the contents of the zip file, and add a reference to each of the unzipped
dlls to your project in Visual Studio (or your IDE of choice).</p>
<p>Official NuGet Packages:
<a class="reference external" href="http://www.nuget.org/List/Packages/Selenium.RC">RC</a>
<a class="reference external" href="http://www.nuget.org/List/Packages/Selenium.WebDriver">WebDriver</a>
<a class="reference external" href="http://www.nuget.org/List/Packages/Selenium.WebDriverBackedSelenium">WebDriverBackedSelenium</a>
<a class="reference external" href="http://www.nuget.org/List/Packages/Selenium.Support">Support</a></p>
</div>
<div class="section" id="python">
<h3>Python<a class="headerlink" href="#python" title="Permalink to this headline">¶</a></h3>
<p>If you are using Python for test automation then you probably are already familiar with developing
in Python.  To add Selenium to your Python environment run the following command from
a command-line.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>pip install selenium
</pre></div>
</div>
<p>Pip requires <a class="reference external" href="http://pypi.python.org/pypi/pip">pip</a> to be installed, pip also has a dependency
on <a class="reference external" href="http://pypi.python.org/pypi/setuptools">setuptools</a>.</p>
<p>Teaching Python development itself is beyond the scope of this document, however there are many
resources on Python and likely developers in your organization can help you get up to speed.</p>
</div>
<div class="section" id="ruby">
<h3>Ruby<a class="headerlink" href="#ruby" title="Permalink to this headline">¶</a></h3>
<p>If you are using Ruby for test automation then you probably are already familiar with developing
in Ruby.  To add Selenium to your Ruby environment run the following command from
a command-line.</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>gem install selenium-webdriver
</pre></div>
</div>
<p>Teaching Ruby development itself is beyond the scope of this document, however there are many
resources on Ruby and likely developers in your organization can help you get up to speed.</p>
</div>
<div class="section" id="perl">
<h3>Perl<a class="headerlink" href="#perl" title="Permalink to this headline">¶</a></h3>
<p>Perl bindings are provided by a third party, please refer to any of their documentation on how to
install / get started. There is one known <a class="reference external" href="https://metacpan.org/module/Selenium::Remote::Driver">Perl binding</a> as of this writing.</p>
</div>
<div class="section" id="php">
<h3>PHP<a class="headerlink" href="#php" title="Permalink to this headline">¶</a></h3>
<p>PHP bindings are provided by a third party, please refer to any of their documentation on how to
install / get started. There are three known bindings at this time: <a class="reference external" href="https://github.com/chibimagic/WebDriver-PHP/">By Chibimagic</a>
<a class="reference external" href="http://code.google.com/p/php-webdriver-bindings/">By Lukasz Kolczynski</a> and <a class="reference external" href="https://github.com/facebook/php-webdriver">By the Facebook</a></p>
</div>
<div class="section" id="javascript">
<h3>JavaScript<a class="headerlink" href="#javascript" title="Permalink to this headline">¶</a></h3>
<p>If you are using node.js to develop your application, the selenium project provides <a class="reference external" href="https://www.npmjs.com/package/selenium-webdriver">Javascript bindings on npm</a>.</p>
<p>Install the Javascript bindings with npm:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>npm install selenium-webdriver
</pre></div>
</div>
<p>You can find the <a class="reference external" href="http://seleniumhq.github.io/selenium/docs/api/javascript/index.html">API docs</a> here.</p>
</div>
</div>
<div class="section" id="migrating-from-selenium-1-0">
<h2>Migrating from Selenium 1.0<a class="headerlink" href="#migrating-from-selenium-1-0" title="Permalink to this headline">¶</a></h2>

<p>For those who already have test suites written using Selenium 1.0, we have provided tips on how to
migrate your existing code to Selenium 2.0.  Simon Stewart, the lead developer for Selenium 2.0,
has written an article on migrating from Selenium 1.0.  We’ve included this as an appendix.</p>
<p>对于那些已经使用Selenium 1.0编写测试套件的人，我们提供了关于如何将现有代码迁移到Selenium 2.0的提示。  Selenium 2.0的首席开发人员Simon Stewart撰写了一篇关于从Selenium 1.0迁移的文章。 我们已经把它作为一个附录。</p>

<p><a class="reference internal" href="appendix_migrating_from_rc_to_webdriver.jsp#migrating-to-webdriver-reference"><span class="std std-ref">Migrating From Selenium RC to Selenium WebDriver</span></a></p>
</div>
<div class="section" id="introducing-the-selenium-webdriver-api-by-example">
<h2>Introducing the Selenium-WebDriver API by Example<a class="headerlink" href="#introducing-the-selenium-webdriver-api-by-example" title="Permalink to this headline">¶</a></h2>
<p>WebDriver is a tool for automating web application testing, and in particular
to verify that they work as expected. It aims to provide a friendly API that’s
easy to explore and understand, easier to use than the Selenium-RC (1.0) API,
which will help to make your tests easier to
read and maintain. It’s not tied to any particular test framework, so it can
be used equally well in a unit testing project or from a plain old “main” method.
This section introduces WebDriver’s API and helps get you started becoming
familiar with it.  Start by setting up a WebDriver project if you haven’t already.
This was described in the previous section, <a class="reference internal" href="#setting-up-webdriver-project"><span class="std std-ref">Setting Up a Selenium-WebDriver Project</span></a>.</p>
<p>WebDriver是一个自动化Web应用程序测试的工具，特别是验证它们是否按预期工作。 它的目的是提供一个友好的API，它易于探索和理解，比Selenium-RC（1.0）API更易于使用，这将有助于使测试更易于阅读和维护。 它不受任何特定的测试框架的束缚，因此它可以在单元测试项目中使用，也可以从简单的旧“主要”方法中使用。 本节介绍WebDriver的API，并帮助您开始熟悉它。 如果你还没有开始建立一个WebDriver项目。 这在前面的章节“设置Selenium-WebDriver项目”中有所描述。</p>


<p>Once your project is set up, you can see that WebDriver acts just as any normal library:
it is entirely self-contained, and you usually don’t need to remember to start any
additional processes or run any installers before using it, as opposed to the proxy server
with Selenium-RC.</p>
<p>一旦你的项目设置好了，你可以看到WebDriver的行为与普通的库相同：它完全是独立的，在使用之前通常不需要记住启动任何额外的进程或运行任何安装程序，相反， 到Selenium-RC的代理服务器。</p>



<p>Note: additional steps are required to use <a class="reference internal" href="#chromedriver">ChromeDriver</a>, <a class="reference internal" href="#opera-driver">Opera Driver</a>, <a class="reference internal" href="#android-driver">Android Driver</a>
and <a class="reference internal" href="#ios-driver">iOS Driver</a></p>
<p>注意：使用ChromeDriver，Opera驱动程序，Android驱动程序和iOS驱动程序需要执行其他步骤</p>

<p>You’re now ready to write some code. An easy way to get started is this
example, which searches for the term “Cheese” on Google and then outputs the
result page’s title to the console.</p>
<p>你现在准备写一些代码。 一个简单的方法就是这个例子，它在Google上搜索术语“Cheese”，然后将结果页面的标题输出到控制台。</p>


<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="kn">package</span> <span class="nn">org.openqa.selenium.example</span><span class="o">;</span>

<span class="kn">import</span> <span class="nn">org.openqa.selenium.By</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.openqa.selenium.WebDriver</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.openqa.selenium.WebElement</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.openqa.selenium.firefox.FirefoxDriver</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.openqa.selenium.support.ui.ExpectedCondition</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.openqa.selenium.support.ui.WebDriverWait</span><span class="o">;</span>

<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Selenium2Example</span>  <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="c1">// Create a new instance of the Firefox driver</span>
        <span class="c1">// Notice that the remainder of the code relies on the interface, </span>
        <span class="c1">// not the implementation.</span>
        <span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">();</span>

        <span class="c1">// And now use this to visit Google</span>
        <span class="n">driver</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"http://www.google.com"</span><span class="o">);</span>
        <span class="c1">// Alternatively the same thing can be done like this</span>
        <span class="c1">// driver.navigate().to("http://www.google.com");</span>

        <span class="c1">// Find the text input element by its name</span>
        <span class="n">WebElement</span> <span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">name</span><span class="o">(</span><span class="s">"q"</span><span class="o">));</span>

        <span class="c1">// Enter something to search for</span>
        <span class="n">element</span><span class="o">.</span><span class="na">sendKeys</span><span class="o">(</span><span class="s">"Cheese!"</span><span class="o">);</span>

        <span class="c1">// Now submit the form. WebDriver will find the form for us from the element</span>
        <span class="n">element</span><span class="o">.</span><span class="na">submit</span><span class="o">();</span>

        <span class="c1">// Check the title of the page</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Page title is: "</span> <span class="o">+</span> <span class="n">driver</span><span class="o">.</span><span class="na">getTitle</span><span class="o">());</span>
        
        <span class="c1">// Google's search is rendered dynamically with JavaScript.</span>
        <span class="c1">// Wait for the page to load, timeout after 10 seconds</span>
        <span class="o">(</span><span class="k">new</span> <span class="n">WebDriverWait</span><span class="o">(</span><span class="n">driver</span><span class="o">,</span> <span class="mi">10</span><span class="o">)).</span><span class="na">until</span><span class="o">(</span><span class="k">new</span> <span class="n">ExpectedCondition</span><span class="o">&lt;</span><span class="n">Boolean</span><span class="o">&gt;()</span> <span class="o">{</span>
            <span class="kd">public</span> <span class="n">Boolean</span> <span class="nf">apply</span><span class="o">(</span><span class="n">WebDriver</span> <span class="n">d</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">return</span> <span class="n">d</span><span class="o">.</span><span class="na">getTitle</span><span class="o">().</span><span class="na">toLowerCase</span><span class="o">().</span><span class="na">startsWith</span><span class="o">(</span><span class="s">"cheese!"</span><span class="o">);</span>
            <span class="o">}</span>
        <span class="o">});</span>

        <span class="c1">// Should see: "cheese! - Google Search"</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"Page title is: "</span> <span class="o">+</span> <span class="n">driver</span><span class="o">.</span><span class="na">getTitle</span><span class="o">());</span>
        
        <span class="c1">//Close the browser</span>
        <span class="n">driver</span><span class="o">.</span><span class="na">quit</span><span class="o">();</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="k">using</span> <span class="nn">OpenQA.Selenium</span><span class="p">;</span>
<span class="k">using</span> <span class="nn">OpenQA.Selenium.Firefox</span><span class="p">;</span>

<span class="c1">// Requires reference to WebDriver.Support.dll</span>
<span class="k">using</span> <span class="nn">OpenQA.Selenium.Support.UI</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">GoogleSuggest</span>
<span class="p">{</span>
    <span class="k">static</span> <span class="k">void</span> <span class="nf">Main</span><span class="p">(</span><span class="kt">string</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// Create a new instance of the Firefox driver.</span>
        <span class="c1">// Note that it is wrapped in a using clause so that the browser is closed </span>
        <span class="c1">// and the webdriver is disposed (even in the face of exceptions).</span>

        <span class="c1">// Also note that the remainder of the code relies on the interface, </span>
        <span class="c1">// not the implementation.</span>

        <span class="c1">// Further note that other drivers (InternetExplorerDriver,</span>
        <span class="c1">// ChromeDriver, etc.) will require further configuration </span>
        <span class="c1">// before this example will work. See the wiki pages for the</span>
        <span class="c1">// individual drivers at http://code.google.com/p/selenium/wiki</span>
        <span class="c1">// for further information.</span>
        <span class="k">using</span> <span class="p">(</span><span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="p">())</span>
        <span class="p">{</span>
            <span class="c1">//Notice navigation is slightly different than the Java version</span>
            <span class="c1">//This is because 'get' is a keyword in C#</span>
            <span class="n">driver</span><span class="p">.</span><span class="n">Navigate</span><span class="p">().</span><span class="n">GoToUrl</span><span class="p">(</span><span class="s">"http://www.google.com/"</span><span class="p">);</span>
    
            <span class="c1">// Find the text input element by its name</span>
            <span class="n">IWebElement</span> <span class="n">query</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">Name</span><span class="p">(</span><span class="s">"q"</span><span class="p">));</span>
    
            <span class="c1">// Enter something to search for</span>
            <span class="n">query</span><span class="p">.</span><span class="n">SendKeys</span><span class="p">(</span><span class="s">"Cheese"</span><span class="p">);</span>
    
            <span class="c1">// Now submit the form. WebDriver will find the form for us from the element</span>
            <span class="n">query</span><span class="p">.</span><span class="n">Submit</span><span class="p">();</span>
    
            <span class="c1">// Google's search is rendered dynamically with JavaScript.</span>
            <span class="c1">// Wait for the page to load, timeout after 10 seconds</span>
            <span class="kt">var</span> <span class="n">wait</span> <span class="p">=</span> <span class="k">new</span> <span class="n">WebDriverWait</span><span class="p">(</span><span class="n">driver</span><span class="p">,</span> <span class="n">TimeSpan</span><span class="p">.</span><span class="n">FromSeconds</span><span class="p">(</span><span class="m">10</span><span class="p">));</span>
            <span class="n">wait</span><span class="p">.</span><span class="n">Until</span><span class="p">(</span><span class="n">d</span> <span class="p">=&gt;</span> <span class="n">d</span><span class="p">.</span><span class="n">Title</span><span class="p">.</span><span class="n">StartsWith</span><span class="p">(</span><span class="s">"cheese"</span><span class="p">,</span> <span class="n">StringComparison</span><span class="p">.</span><span class="n">OrdinalIgnoreCase</span><span class="p">));</span>
    
            <span class="c1">// Should see: "Cheese - Google Search" (for an English locale)</span>
            <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="s">"Page title is: "</span> <span class="p">+</span> <span class="n">driver</span><span class="p">.</span><span class="n">Title</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">webdriver</span>
<span class="kn">from</span> <span class="nn">selenium.common.exceptions</span> <span class="kn">import</span> <span class="n">TimeoutException</span>
<span class="kn">from</span> <span class="nn">selenium.webdriver.support.ui</span> <span class="kn">import</span> <span class="n">WebDriverWait</span> <span class="c1"># available since 2.4.0</span>
<span class="kn">from</span> <span class="nn">selenium.webdriver.support</span> <span class="kn">import</span> <span class="n">expected_conditions</span> <span class="k">as</span> <span class="n">EC</span> <span class="c1"># available since 2.26.0</span>

<span class="c1"># Create a new instance of the Firefox driver</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Firefox</span><span class="p">()</span>

<span class="c1"># go to the google home page</span>
<span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"http://www.google.com"</span><span class="p">)</span>

<span class="c1"># the page is ajaxy so the title is originally this:</span>
<span class="k">print</span> <span class="n">driver</span><span class="o">.</span><span class="n">title</span>

<span class="c1"># find the element that's name attribute is q (the google search box)</span>
<span class="n">inputElement</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s2">"q"</span><span class="p">)</span>

<span class="c1"># type in the search</span>
<span class="n">inputElement</span><span class="o">.</span><span class="n">send_keys</span><span class="p">(</span><span class="s2">"cheese!"</span><span class="p">)</span>

<span class="c1"># submit the form (although google automatically searches now without submitting)</span>
<span class="n">inputElement</span><span class="o">.</span><span class="n">submit</span><span class="p">()</span>

<span class="k">try</span><span class="p">:</span>
    <span class="c1"># we have to wait for the page to refresh, the last thing that seems to be updated is the title</span>
    <span class="n">WebDriverWait</span><span class="p">(</span><span class="n">driver</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">until</span><span class="p">(</span><span class="n">EC</span><span class="o">.</span><span class="n">title_contains</span><span class="p">(</span><span class="s2">"cheese!"</span><span class="p">))</span>

    <span class="c1"># You should see "cheese! - Google Search"</span>
    <span class="k">print</span> <span class="n">driver</span><span class="o">.</span><span class="n">title</span>

<span class="k">finally</span><span class="p">:</span>
    <span class="n">driver</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="nb">require</span> <span class="s1">'rubygems'</span>
<span class="nb">require</span> <span class="s1">'selenium-webdriver'</span>

<span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:firefox</span>
<span class="n">driver</span><span class="o">.</span><span class="n">get</span> <span class="s2">"http://google.com"</span>

<span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span> <span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s2">"q"</span>
<span class="n">element</span><span class="o">.</span><span class="n">send_keys</span> <span class="s2">"Cheese!"</span>
<span class="n">element</span><span class="o">.</span><span class="n">submit</span>

<span class="nb">puts</span> <span class="s2">"Page title is </span><span class="se">#</span><span class="s2">{driver.title}"</span>

<span class="n">wait</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Wait</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:timeout</span> <span class="o">=&gt;</span> <span class="mi">10</span><span class="p">)</span>
<span class="n">wait</span><span class="o">.</span><span class="n">until</span> <span class="p">{</span> <span class="n">driver</span><span class="o">.</span><span class="n">title</span><span class="o">.</span><span class="n">downcase</span><span class="o">.</span><span class="n">start_with?</span> <span class="s2">"cheese!"</span> <span class="p">}</span>

<span class="nb">puts</span> <span class="s2">"Page title is </span><span class="se">#</span><span class="s2">{driver.title}"</span>
<span class="n">driver</span><span class="o">.</span><span class="n">quit</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">webdriver</span><span class="p">.</span><span class="nx">Builder</span><span class="p">().</span><span class="nx">build</span><span class="p">();</span>
<span class="nx">driver</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">'http://www.google.com'</span><span class="p">);</span>

<span class="kd">var</span> <span class="nx">element</span> <span class="o">=</span> <span class="nx">driver</span><span class="p">.</span><span class="nx">findElement</span><span class="p">(</span><span class="nx">webdriver</span><span class="p">.</span><span class="nx">By</span><span class="p">.</span><span class="nx">name</span><span class="p">(</span><span class="s1">'q'</span><span class="p">));</span>
<span class="nx">element</span><span class="p">.</span><span class="nx">sendKeys</span><span class="p">(</span><span class="s1">'Cheese!'</span><span class="p">);</span>
<span class="nx">element</span><span class="p">.</span><span class="nx">submit</span><span class="p">();</span>

<span class="nx">driver</span><span class="p">.</span><span class="nx">getTitle</span><span class="p">().</span><span class="nx">then</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">title</span><span class="p">)</span> <span class="p">{</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Page title is: '</span> <span class="o">+</span> <span class="nx">title</span><span class="p">);</span>
<span class="p">});</span>

<span class="nx">driver</span><span class="p">.</span><span class="nx">wait</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">return</span> <span class="nx">driver</span><span class="p">.</span><span class="nx">getTitle</span><span class="p">().</span><span class="nx">then</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">title</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">title</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">().</span><span class="nx">lastIndexOf</span><span class="p">(</span><span class="s1">'cheese!'</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">===</span> <span class="mi">0</span><span class="p">;</span>
  <span class="p">});</span>
<span class="p">},</span> <span class="mi">3000</span><span class="p">);</span>

<span class="nx">driver</span><span class="p">.</span><span class="nx">getTitle</span><span class="p">().</span><span class="nx">then</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">title</span><span class="p">)</span> <span class="p">{</span>
  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">'Page title is: '</span> <span class="o">+</span> <span class="nx">title</span><span class="p">);</span>
<span class="p">});</span>

<span class="nx">driver</span><span class="p">.</span><span class="nx">quit</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="k">use</span> <span class="nn">Selenium::Remote::Driver</span><span class="p">;</span>

<span class="c1"># Create a new instance of the driver</span>
<span class="k">my</span> <span class="nv">$driver</span> <span class="o">=</span> <span class="nn">Selenium::Remote::Driver</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">;</span>

<span class="c1"># Go to the google homepage</span>
<span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">get</span><span class="p">(</span><span class="s">"http://www.google.com"</span><span class="p">);</span>

<span class="c1"># Print original page title</span>
<span class="k">my</span> <span class="nv">$title</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">get_title</span><span class="p">();</span>
<span class="k">print</span> <span class="s">"$title\n"</span><span class="p">;</span>

<span class="c1"># Find the element that's name attribute is q (google search box)</span>
<span class="k">my</span> <span class="nv">$inputElement</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_element</span><span class="p">(</span><span class="s">"q"</span><span class="p">,</span> <span class="s">"name"</span><span class="p">);</span>

<span class="c1"># type in the search</span>
<span class="nv">$inputElement</span><span class="o">-&gt;</span><span class="n">send_keys</span><span class="p">(</span><span class="s">"cheese!"</span><span class="p">);</span>

<span class="c1"># submit the form (although google automatically searches now without submitting)</span>
<span class="nv">$inputElement</span><span class="o">-&gt;</span><span class="n">submit</span><span class="p">();</span>

<span class="c1"># Set the timeout for searching for elements to 10 seconds (0 by default)</span>
<span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">set_implicit_wait_timeout</span><span class="p">(</span><span class="mi">10000</span><span class="p">);</span>
<span class="c1"># then use XPath to search for a page title containing cheese!</span>
<span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_element</span><span class="p">(</span><span class="s">"/html/head/title[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'cheese!')]"</span><span class="p">);</span>

<span class="c1"># You should see "cheese! - Google Search"</span>
<span class="nv">$title</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">get_title</span><span class="p">();</span>
<span class="k">print</span> <span class="s">"$title\n"</span><span class="p">;</span>

<span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">quit</span><span class="p">();</span>
</pre></div>
</div>
<p>In upcoming sections, you will learn more about how to use WebDriver for things
such as navigating forward and backward in your browser’s history, and how to
test web sites that use frames and windows. We also provide a more
thorough discussions and examples.</p>
<p>在即将到来的章节中，您将学习更多关于如何使用WebDriver来浏览浏览器历史记录中的向前和向后浏览以及如何测试使用框架和窗口的网站。 我们也提供一个更彻底的讨论和例子。</p>

</div>
<div class="section" id="selenium-webdriver-api-commands-and-operations">
<h2>Selenium-WebDriver API Commands and Operations<a class="headerlink" href="#selenium-webdriver-api-commands-and-operations" title="Permalink to this headline">¶</a></h2>
<div class="section" id="fetching-a-page">
<h3>Fetching a Page<a class="headerlink" href="#fetching-a-page" title="Permalink to this headline">¶</a></h3>
<p>The first thing you’re likely to want to do with WebDriver is navigate to a page.
The normal way to do this is by calling “get”:</p>
<p>WebDriver可能要做的第一件事就是导航到一个页面。 正常的做法是通过调用“get”</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"http://www.google.com"</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="p">.</span><span class="n">Url</span> <span class="p">=</span> <span class="s">"http://www.google.com"</span><span class="p">;</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">get</span> <span class="s2">"http://www.google.com"</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"http://www.google.com"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">get</span><span class="p">(</span><span class="s">'http://www.google.com'</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="nx">driver</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="s1">'http://www.google.com'</span><span class="p">);</span>
</pre></div>
</div>
<p>Dependent on several factors, including the OS/Browser combination,
WebDriver may or may not wait for the page to load. In some circumstances,
WebDriver may return control before the page has finished, or even started, loading.
To ensure robustness, you need to wait for the element(s) to exist in the page using
<a class="reference internal" href="04_webdriver_advanced.jsp#explicit-and-implicit-waits-reference"><span class="std std-ref">Explicit and Implicit Waits</span></a>.</p>
<p>WebDriver取决于多个因素，包括OS / Browser组合，WebDriver可能会或可能不会等待页面加载。 在某些情况下，WebDriver可能会在页面完成之前返回控件，甚至开始加载。 为确保健壮性，您需要使用显式和隐式等待来等待页面中存在的元素。</p>


</div>
<div class="section" id="locating-ui-elements-webelements">
<h3>Locating UI Elements (WebElements)<a class="headerlink" href="#locating-ui-elements-webelements" title="Permalink to this headline">¶</a></h3>
<p>Locating elements in WebDriver can be done on the WebDriver instance itself or on a WebElement.
Each of the language bindings exposes a “Find Element” and “Find Elements” method. The former returns
a WebElement object matching the query, and throws an exception if such an element cannot be found.
The latter returns a list of WebElements, possibly empty if no DOM elements match the query.</p>
<p>在WebDriver中定位元素可以在WebDriver实例本身或WebElement上完成。 每种语言绑定都公开了“查找元素”和“查找元素”方法。 前者返回与查询相匹配的WebElement对象，如果找不到这样的元素则抛出异常。 后者返回WebElements的列表，如果没有DOM元素匹配查询，则可能为空。</p>


<p>The “Find” methods take a locator or query object called “By”. “By” strategies are listed below.</p>
<p>“查找”方法使用称为“By”的定位器或查询对象。  “按”策略列在下面。</p>


<div class="section" id="by-id">
<h4>By ID<a class="headerlink" href="#by-id" title="Permalink to this headline">¶</a></h4>
<p>This is the most efficient and preferred way to locate an element. Common pitfalls that UI developers
make is having non-unique id’s on a page or auto-generating the id, both should be avoided. A class
on an html element is more appropriate than an auto-generated id.</p>
<p>这是定位元素的最有效和首选的方法。  UI开发人员常犯的错误是在页面上使用非唯一的id或自动生成id，这两者都应该避免。  html元素上的类比自动生成的id更合适。</p>

<p>Example of how to find an element that looks like this:</p>
<p>如何找到这样的元素的例子</p>


<div class="highlight-html"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">"coolestWidgetEvah"</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebElement</span> <span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="s">"coolestWidgetEvah"</span><span class="o">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebElement</span> <span class="n">element</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">Id</span><span class="p">(</span><span class="s">"coolestWidgetEvah"</span><span class="p">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:id</span><span class="p">,</span> <span class="s2">"coolestWidgetEvah"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_id</span><span class="p">(</span><span class="s2">"coolestWidgetEvah"</span><span class="p">)</span>

<span class="ow">or</span>

<span class="kn">from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="kn">import</span> <span class="n">By</span>
<span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="n">By</span><span class="o">.</span><span class="n">ID</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s2">"coolestWidgetEvah"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$element</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_element</span><span class="p">(</span><span class="s">'coolestWidgetEvah'</span><span class="p">,</span><span class="s">'id'</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">element</span> <span class="o">=</span> <span class="nx">driver</span><span class="p">.</span><span class="nx">findElement</span><span class="p">(</span><span class="nx">By</span><span class="p">.</span><span class="nx">id</span><span class="p">(</span><span class="s1">'coolestWidgetEvah'</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="by-class-name">
<h4>By Class Name<a class="headerlink" href="#by-class-name" title="Permalink to this headline">¶</a></h4>
<p>“Class” in this case refers to the attribute on the DOM element. Often in practical use there are
many DOM elements with the same class name, thus finding multiple elements becomes the more practical
option over finding the first element.</p>
<p>在这种情况下，“类”是指DOM元素上的属性。 通常在实际应用中，有许多具有相同类名的DOM元素，因此找到多个元素成为寻找第一个元素的更实际的选择。</p>


<p>Example of how to find an element that looks like this:</p>
<p>如何找到这样的元素的例子</p>


<div class="highlight-html"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"cheese"</span><span class="p">&gt;&lt;</span><span class="nt">span</span><span class="p">&gt;</span>Cheddar<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;&lt;/</span><span class="nt">div</span><span class="p">&gt;&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"cheese"</span><span class="p">&gt;&lt;</span><span class="nt">span</span><span class="p">&gt;</span>Gouda<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">List</span><span class="o">&lt;</span><span class="n">WebElement</span><span class="o">&gt;</span> <span class="n">cheeses</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElements</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">className</span><span class="o">(</span><span class="s">"cheese"</span><span class="o">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IList</span><span class="p">&lt;</span><span class="n">IWebElement</span><span class="p">&gt;</span> <span class="n">cheeses</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElements</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">ClassName</span><span class="p">(</span><span class="s">"cheese"</span><span class="p">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">cheeses</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_elements</span><span class="p">(</span><span class="ss">:class_name</span><span class="p">,</span> <span class="s2">"cheese"</span><span class="p">)</span>

<span class="ow">or</span>

<span class="n">cheeses</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_elements</span><span class="p">(</span><span class="ss">:class</span><span class="p">,</span> <span class="s2">"cheese"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">cheeses</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_elements_by_class_name</span><span class="p">(</span><span class="s2">"cheese"</span><span class="p">)</span>

<span class="ow">or</span>

<span class="kn">from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="kn">import</span> <span class="n">By</span>
<span class="n">cheeses</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_elements</span><span class="p">(</span><span class="n">By</span><span class="o">.</span><span class="n">CLASS_NAME</span><span class="p">,</span> <span class="s2">"cheese"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">@cheeses</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_elements</span><span class="p">(</span><span class="s">'cheese'</span><span class="p">,</span> <span class="s">'class'</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="nx">driver</span><span class="p">.</span><span class="nx">findElements</span><span class="p">(</span><span class="nx">By</span><span class="p">.</span><span class="nx">className</span><span class="p">(</span><span class="s2">"cheese"</span><span class="p">))</span>
<span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">cheeses</span> <span class="p">=&gt;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">cheeses</span><span class="p">.</span><span class="nx">length</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="by-tag-name">
<h4>By Tag Name<a class="headerlink" href="#by-tag-name" title="Permalink to this headline">¶</a></h4>
<p>The DOM Tag Name of the element.</p>
<p>元素的DOM标签名称。</p>

<p>Example of how to find an element that looks like this:</p>
<p>如何找到这样的元素的例子</p>

<div class="highlight-html"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">iframe</span> <span class="na">src</span><span class="o">=</span><span class="s">"..."</span><span class="p">&gt;&lt;/</span><span class="nt">iframe</span><span class="p">&gt;</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebElement</span> <span class="n">frame</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">tagName</span><span class="o">(</span><span class="s">"iframe"</span><span class="o">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebElement</span> <span class="n">frame</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">TagName</span><span class="p">(</span><span class="s">"iframe"</span><span class="p">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">frame</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:tag_name</span><span class="p">,</span> <span class="s2">"iframe"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">frame</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_tag_name</span><span class="p">(</span><span class="s2">"iframe"</span><span class="p">)</span>

<span class="ow">or</span>

<span class="kn">from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="kn">import</span> <span class="n">By</span>
<span class="n">frame</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="n">By</span><span class="o">.</span><span class="n">TAG_NAME</span><span class="p">,</span> <span class="s2">"iframe"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$frame</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_element</span><span class="p">(</span><span class="s">'iframe'</span><span class="p">,</span> <span class="s">'tag_name'</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">frame</span> <span class="o">=</span> <span class="nx">driver</span><span class="p">.</span><span class="nx">findElement</span><span class="p">(</span><span class="nx">By</span><span class="p">.</span><span class="nx">tagName</span><span class="p">(</span><span class="s1">'iframe'</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="by-name">
<h4>By Name<a class="headerlink" href="#by-name" title="Permalink to this headline">¶</a></h4>
<p>Find the input element with matching name attribute.</p>
<p>找到匹配name属性的输入元素。</p>

<p>Example of how to find an element that looks like this:</p>
<p>如何找到这样的元素的例子</p>

<div class="highlight-html"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">input</span> <span class="na">name</span><span class="o">=</span><span class="s">"cheese"</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span><span class="p">/&gt;</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebElement</span> <span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">name</span><span class="o">(</span><span class="s">"cheese"</span><span class="o">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebElement</span> <span class="n">cheese</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">Name</span><span class="p">(</span><span class="s">"cheese"</span><span class="p">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:name</span><span class="p">,</span> <span class="s2">"cheese"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s2">"cheese"</span><span class="p">)</span>

<span class="ow">or</span>

<span class="kn">from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="kn">import</span> <span class="n">By</span>
<span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="n">By</span><span class="o">.</span><span class="n">NAME</span><span class="p">,</span> <span class="s2">"cheese"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$cheese</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_element</span><span class="p">(</span><span class="s">'cheese'</span><span class="p">,</span> <span class="s">'name'</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">cheese</span> <span class="o">=</span> <span class="nx">driver</span><span class="p">.</span><span class="nx">findElement</span><span class="p">(</span><span class="nx">By</span><span class="p">.</span><span class="nx">name</span><span class="p">(</span><span class="s1">'cheese'</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="by-link-text">
<h4>By Link Text<a class="headerlink" href="#by-link-text" title="Permalink to this headline">¶</a></h4>
<p>Find the link element with matching visible text.</p>
<p>找到匹配可见文本的链接元素。</p>

<p>Example of how to find an element that looks like this:</p>
<p>如何找到这样的元素的例子</p>

<div class="highlight-html"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"http://www.google.com/search?q=cheese"</span><span class="p">&gt;</span>cheese<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>&gt;
</pre></div>
</div>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebElement</span> <span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">linkText</span><span class="o">(</span><span class="s">"cheese"</span><span class="o">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebElement</span> <span class="n">cheese</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">LinkText</span><span class="p">(</span><span class="s">"cheese"</span><span class="p">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:link_text</span><span class="p">,</span> <span class="s2">"cheese"</span><span class="p">)</span>

<span class="ow">or</span>

<span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:link</span><span class="p">,</span> <span class="s2">"cheese"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_link_text</span><span class="p">(</span><span class="s2">"cheese"</span><span class="p">)</span>

<span class="ow">or</span>

<span class="kn">from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="kn">import</span> <span class="n">By</span>
<span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="n">By</span><span class="o">.</span><span class="n">LINK_TEXT</span><span class="p">,</span> <span class="s2">"cheese"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$cheese</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_element</span><span class="p">(</span><span class="s">'cheese'</span><span class="p">,</span> <span class="s">'link_text'</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">cheese</span> <span class="o">=</span> <span class="nx">driver</span><span class="p">.</span><span class="nx">findElement</span><span class="p">(</span><span class="nx">By</span><span class="p">.</span><span class="nx">linkText</span><span class="p">(</span><span class="s1">'cheese'</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="by-partial-link-text">
<h4>By Partial Link Text<a class="headerlink" href="#by-partial-link-text" title="Permalink to this headline">¶</a></h4>
<p>Find the link element with partial matching visible text.</p>
<p>找到部分匹配可见文本的链接元素。</p>

<p>Example of how to find an element that looks like this:</p>
<p>如何找到这样的元素的例子</p>

<div class="highlight-html"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"http://www.google.com/search?q=cheese"</span><span class="p">&gt;</span>search for cheese<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>&gt;
</pre></div>
</div>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebElement</span> <span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">partialLinkText</span><span class="o">(</span><span class="s">"cheese"</span><span class="o">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebElement</span> <span class="n">cheese</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">PartialLinkText</span><span class="p">(</span><span class="s">"cheese"</span><span class="p">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:partial_link_text</span><span class="p">,</span> <span class="s2">"cheese"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_partial_link_text</span><span class="p">(</span><span class="s2">"cheese"</span><span class="p">)</span>

<span class="ow">or</span>

<span class="kn">from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="kn">import</span> <span class="n">By</span>
<span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="n">By</span><span class="o">.</span><span class="n">PARTIAL_LINK_TEXT</span><span class="p">,</span> <span class="s2">"cheese"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$cheese</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_element</span><span class="p">(</span><span class="s">'cheese'</span><span class="p">,</span> <span class="s">'partial_link_text'</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">cheese</span> <span class="o">=</span> <span class="nx">driver</span><span class="p">.</span><span class="nx">findElement</span><span class="p">(</span><span class="nx">By</span><span class="p">.</span><span class="nx">partialLinkText</span><span class="p">(</span><span class="s1">'cheese'</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="by-css">
<h4>By CSS<a class="headerlink" href="#by-css" title="Permalink to this headline">¶</a></h4>
<p>Like the name implies it is a locator strategy by css. Native browser support
is used by default, so please refer to <a class="reference external" href="http://www.w3.org/TR/CSS/#selectors">w3c css selectors</a> for a list of generally available css selectors. If a browser does not have
native support for css queries, then <a class="reference external" href="http://sizzlejs.com/">Sizzle</a> is used. IE 6,7 and FF3.0
currently use Sizzle as the css query engine.</p>
<p>就像这个名字所暗示的那样，它是一个由css定位的策略。 默认情况下使用本地浏览器支持，所以请参阅w3c css选择器以获取一般可用的css选择器列表。 如果浏览器不支持css查询，则使用Sizzle。  IE 67和FF3.0目前使用Sizzle作为css查询引擎。</p>


<p>Beware that not all browsers were created equal, some css that might work in one version may not work
in another.</p>
<p>注意并不是所有的浏览器都是平等的，一些可能在一个版本中工作的CSS可能不能在另一个版本中工作。</p>

<p>Example of to find the cheese below:</p>
<p>下面找到奶酪的例子</p>

<div class="highlight-html"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">"food"</span><span class="p">&gt;&lt;</span><span class="nt">span</span> <span class="na">class</span><span class="o">=</span><span class="s">"dairy"</span><span class="p">&gt;</span>milk<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;&lt;</span><span class="nt">span</span> <span class="na">class</span><span class="o">=</span><span class="s">"dairy aged"</span><span class="p">&gt;</span>cheese<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebElement</span> <span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">cssSelector</span><span class="o">(</span><span class="s">"#food span.dairy.aged"</span><span class="o">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebElement</span> <span class="n">cheese</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">CssSelector</span><span class="p">(</span><span class="s">"#food span.dairy.aged"</span><span class="p">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:css</span><span class="p">,</span> <span class="s2">"#food span.dairy.aged"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_css_selector</span><span class="p">(</span><span class="s2">"#food span.dairy.aged"</span><span class="p">)</span>

<span class="ow">or</span>

<span class="kn">from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="kn">import</span> <span class="n">By</span>
<span class="n">cheese</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="n">By</span><span class="o">.</span><span class="n">CSS_SELECTOR</span><span class="p">,</span> <span class="s2">"#food span.dairy.aged"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$cheese</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_element</span><span class="p">(</span><span class="s">'#food span.dairy.aged'</span><span class="p">,</span> <span class="s">'css'</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">cheese</span> <span class="o">=</span> <span class="nx">driver</span><span class="p">.</span><span class="nx">findElement</span><span class="p">(</span><span class="nx">By</span><span class="p">.</span><span class="nx">css</span><span class="p">(</span><span class="s1">'#food span.dairy.aged'</span><span class="p">));</span>
</pre></div>
</div>
</div>
<div class="section" id="by-xpath">
<h4>By XPath<a class="headerlink" href="#by-xpath" title="Permalink to this headline">¶</a></h4>
<p>At a high level, WebDriver uses a browser’s native XPath capabilities wherever
possible. On those browsers that don’t have native XPath support, we have
provided our own implementation. This can lead to some unexpected behaviour
unless you are aware of the differences in the various XPath engines.</p>
<p>在很高的层面上，WebDriver尽可能使用浏览器的本地XPath功能。 在那些没有本地XPath支持的浏览器上，我们提供了自己的实现。 这可能会导致一些意外的行为，除非您知道各种XPath引擎的差异。</p>


<table border="1" class="docutils">
<colgroup>
<col width="28%">
<col width="24%">
<col width="27%">
<col width="21%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Driver</th>
<th class="head">Tag and Attribute Name</th>
<th class="head">Attribute Values</th>
<th class="head">Native XPath Support</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><a class="reference internal" href="#htmlunit-driver">HtmlUnit Driver</a></td>
<td>Lower-cased</td>
<td>As they appear in the HTML</td>
<td>Yes</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#internet-explorer-driver">Internet Explorer Driver</a></td>
<td>Lower-cased</td>
<td>As they appear in the HTML</td>
<td>No</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#firefox-driver">Firefox Driver</a></td>
<td>Case insensitive</td>
<td>As they appear in the HTML</td>
<td>Yes</td>
</tr>
</tbody>
</table>
<p>This is a little abstract, so for the following piece of HTML:</p>
<p>这是一个小抽象的，所以对于下面的一段HTML</p>

<div class="highlight-html"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"example"</span> <span class="p">/&gt;</span>
<span class="p">&lt;</span><span class="nt">INPUT</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"other"</span> <span class="p">/&gt;</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">List</span><span class="o">&lt;</span><span class="n">WebElement</span><span class="o">&gt;</span> <span class="n">inputs</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElements</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">xpath</span><span class="o">(</span><span class="s">"//input"</span><span class="o">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IList</span><span class="p">&lt;</span><span class="n">IWebElement</span><span class="p">&gt;</span> <span class="n">inputs</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElements</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">XPath</span><span class="p">(</span><span class="s">"//input"</span><span class="p">));</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">inputs</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_elements</span><span class="p">(</span><span class="ss">:xpath</span><span class="p">,</span> <span class="s2">"//input"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">inputs</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_elements_by_xpath</span><span class="p">(</span><span class="s2">"//input"</span><span class="p">)</span>

<span class="ow">or</span>

<span class="kn">from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="kn">import</span> <span class="n">By</span>
<span class="n">inputs</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_elements</span><span class="p">(</span><span class="n">By</span><span class="o">.</span><span class="n">XPATH</span><span class="p">,</span> <span class="s2">"//input"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">@inputs</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_elements</span><span class="p">(</span><span class="s">'//input'</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="nx">driver</span><span class="p">.</span><span class="nx">findElements</span><span class="p">(</span><span class="nx">By</span><span class="p">.</span><span class="nx">xpath</span><span class="p">(</span><span class="s2">"//input"</span><span class="p">))</span>
<span class="p">.</span><span class="nx">then</span><span class="p">(</span><span class="nx">cheeses</span> <span class="p">=&gt;</span> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">cheeses</span><span class="p">.</span><span class="nx">length</span><span class="p">));</span>
</pre></div>
</div>
<p>The following number of matches will be found</p>
<p>以下数量的比赛将被发现</p>

<table border="1" class="docutils">
<colgroup>
<col width="21%">
<col width="24%">
<col width="24%">
<col width="32%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">XPath expression</th>
<th class="head"><a class="reference internal" href="#htmlunit-driver">HtmlUnit Driver</a></th>
<th class="head"><a class="reference internal" href="#firefox-driver">Firefox Driver</a></th>
<th class="head"><a class="reference internal" href="#internet-explorer-driver">Internet Explorer Driver</a></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>//input</td>
<td>1 (“example”)</td>
<td>2</td>
<td>2</td>
</tr>
<tr class="row-odd"><td>//INPUT</td>
<td>0</td>
<td>2</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>Sometimes HTML elements do not need attributes to be explicitly declared
because they will default to known values. For example, the “input” tag does
not require the “type” attribute because it defaults to “text”. The rule of
thumb when using xpath in WebDriver is that you <strong>should not</strong> expect to be able
to match against these implicit attributes.</p>
<p>有时HTML元素不需要显式声明属性，因为它们默认为已知值。 例如，“输入”标签不需要“类型”属性，因为它默认为“文本”。 在WebDriver中使用xpath时的经验法则是，您不应该期望能够匹配这些隐式属性。</p>


</div>
<div class="section" id="using-javascript">
<h4>Using JavaScript<a class="headerlink" href="#using-javascript" title="Permalink to this headline">¶</a></h4>
<p>You can execute arbitrary javascript to find an element and as long as you return a DOM Element,
it will be automatically converted to a WebElement object.</p>
<p>您可以执行任意的javascript来查找元素，只要您返回一个DOM元素，它就会自动转换为一个WebElement对象。</p>

<p>Simple example on a page that has jQuery loaded:</p>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebElement</span> <span class="n">element</span> <span class="o">=</span> <span class="o">(</span><span class="n">WebElement</span><span class="o">)</span> <span class="o">((</span><span class="n">JavascriptExecutor</span><span class="o">)</span><span class="n">driver</span><span class="o">).</span><span class="na">executeScript</span><span class="o">(</span><span class="s">"return $('.cheese')[0]"</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebElement</span> <span class="n">element</span> <span class="p">=</span> <span class="p">(</span><span class="n">IWebElement</span><span class="p">)</span> <span class="p">((</span><span class="n">IJavaScriptExecutor</span><span class="p">)</span><span class="n">driver</span><span class="p">).</span><span class="n">ExecuteScript</span><span class="p">(</span><span class="s">"return $('.cheese')[0]"</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">execute_script</span><span class="p">(</span><span class="s2">"return $('.cheese')[0]"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">execute_script</span><span class="p">(</span><span class="s2">"return $('.cheese')[0]"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">execute_script</span><span class="p">(</span><span class="s">"return $('.cheese')[0]"</span><span class="p">);</span>
</pre></div>
</div>
<p>Finding all the input elements for every label on a page:</p>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">List</span><span class="o">&lt;</span><span class="n">WebElement</span><span class="o">&gt;</span> <span class="n">labels</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElements</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">tagName</span><span class="o">(</span><span class="s">"label"</span><span class="o">));</span>
<span class="n">List</span><span class="o">&lt;</span><span class="n">WebElement</span><span class="o">&gt;</span> <span class="n">inputs</span> <span class="o">=</span> <span class="o">(</span><span class="n">List</span><span class="o">&lt;</span><span class="n">WebElement</span><span class="o">&gt;)</span> <span class="o">((</span><span class="n">JavascriptExecutor</span><span class="o">)</span><span class="n">driver</span><span class="o">).</span><span class="na">executeScript</span><span class="o">(</span>
    <span class="s">"var labels = arguments[0], inputs = []; for (var i=0; i &lt; labels.length; i++){"</span> <span class="o">+</span>
    <span class="s">"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;"</span><span class="o">,</span> <span class="n">labels</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IList</span><span class="p">&lt;</span><span class="n">IWebElement</span><span class="p">&gt;</span> <span class="n">labels</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElements</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">TagName</span><span class="p">(</span><span class="s">"label"</span><span class="p">));</span>
<span class="n">IList</span><span class="p">&lt;</span><span class="n">IWebElement</span><span class="p">&gt;</span> <span class="n">inputs</span> <span class="p">=</span> <span class="p">(</span><span class="n">IList</span><span class="p">&lt;</span><span class="n">IWebElement</span><span class="p">&gt;)</span> <span class="p">((</span><span class="n">IJavaScriptExecutor</span><span class="p">)</span><span class="n">driver</span><span class="p">).</span><span class="n">ExecuteScript</span><span class="p">(</span>
    <span class="s">"var labels = arguments[0], inputs = []; for (var i=0; i &lt; labels.length; i++){"</span> <span class="p">+</span>
    <span class="s">"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;"</span><span class="p">,</span> <span class="n">labels</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">labels</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_elements</span><span class="p">(</span><span class="ss">:tag_name</span><span class="p">,</span> <span class="s2">"label"</span><span class="p">)</span>
<span class="n">inputs</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">execute_script</span><span class="p">(</span>
    <span class="s2">"var labels = arguments[0], inputs = []; for (var i=0; i &lt; labels.length; i++){"</span> <span class="o">+</span>
    <span class="s2">"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;"</span><span class="p">,</span> <span class="n">labels</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">labels</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_elements_by_tag_name</span><span class="p">(</span><span class="s2">"label"</span><span class="p">)</span>
<span class="n">inputs</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">execute_script</span><span class="p">(</span>
    <span class="s2">"var labels = arguments[0], inputs = []; for (var i=0; i &lt; labels.length; i++){"</span> <span class="o">+</span>
    <span class="s2">"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;"</span><span class="p">,</span> <span class="n">labels</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="k">my</span> <span class="nv">@labels</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_elements</span><span class="p">(</span><span class="s">'label'</span><span class="p">,</span> <span class="s">'tag_name'</span><span class="p">);</span>
<span class="k">my</span> <span class="nv">$inputs</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">execute_script</span><span class="p">(</span><span class="s">"var labels = arguments, inputs = []; for (var i=0; i &lt; labels.length; i++){ inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;"</span><span class="p">,</span> <span class="nv">@labels</span><span class="p">);</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="getting-text-values">
<h3>Getting text values<a class="headerlink" href="#getting-text-values" title="Permalink to this headline">¶</a></h3>
<p>People often wish to retrieve the innerText value contained within an element.  This returns a single string value.  Note that this will only return the visible text displayed on the page.</p>
<p>人们通常希望检索元素中包含的innerText值。 这将返回一个单一的字符串值。 请注意，这只会返回页面上显示的可见文本。</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebElement</span> <span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="s">"elementID"</span><span class="o">));</span>
<span class="n">element</span><span class="o">.</span><span class="na">getText</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebElement</span> <span class="n">element</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">findElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">id</span><span class="p">(</span><span class="s">"elementID"</span><span class="p">));</span>
<span class="n">element</span><span class="p">.</span><span class="n">Text</span><span class="p">;</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span> <span class="ss">:id</span> <span class="o">=&gt;</span> <span class="s2">"element_id"</span>
<span class="n">element</span><span class="o">.</span><span class="n">text</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_id</span><span class="p">(</span><span class="s2">"element_id"</span><span class="p">)</span>
<span class="n">element</span><span class="o">.</span><span class="n">text</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span>var element = driver.findElement(By.id('elementID'));
element.getText().then(text =&gt; console.log(`Text is ${text}`));
</pre></div>
</div>
</div>
<div class="section" id="user-input-filling-in-forms">
<h3>User Input - Filling In Forms<a class="headerlink" href="#user-input-filling-in-forms" title="Permalink to this headline">¶</a></h3>
<p>We’ve already seen how to enter text into a textarea or text field, but what
about the other elements? You can “toggle” the state of checkboxes, and you
can use “click” to set something like an OPTION tag selected. Dealing
with SELECT tags isn’t too bad:</p>
<p>我们已经看到了如何将文本输入到textarea或文本字段，但其他元素呢？ 您可以“切换”复选框的状态，并且可以使用“单击”来设置类似于所选OPTION标记的内容。 处理SELECT标签不算太坏</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebElement</span> <span class="n">select</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">tagName</span><span class="o">(</span><span class="s">"select"</span><span class="o">));</span>
<span class="n">List</span><span class="o">&lt;</span><span class="n">WebElement</span><span class="o">&gt;</span> <span class="n">allOptions</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="na">findElements</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">tagName</span><span class="o">(</span><span class="s">"option"</span><span class="o">));</span>
<span class="k">for</span> <span class="o">(</span><span class="n">WebElement</span> <span class="n">option</span> <span class="o">:</span> <span class="n">allOptions</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">String</span><span class="o">.</span><span class="na">format</span><span class="o">(</span><span class="s">"Value is: %s"</span><span class="o">,</span> <span class="n">option</span><span class="o">.</span><span class="na">getAttribute</span><span class="o">(</span><span class="s">"value"</span><span class="o">)));</span>
    <span class="n">option</span><span class="o">.</span><span class="na">click</span><span class="o">();</span>
<span class="o">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebElement</span> <span class="k">select</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">TagName</span><span class="p">(</span><span class="s">"select"</span><span class="p">));</span>
<span class="n">IList</span><span class="p">&lt;</span><span class="n">IWebElement</span><span class="p">&gt;</span> <span class="n">allOptions</span> <span class="p">=</span> <span class="k">select</span><span class="p">.</span><span class="n">FindElements</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">TagName</span><span class="p">(</span><span class="s">"option"</span><span class="p">));</span>
<span class="k">foreach</span> <span class="p">(</span><span class="n">IWebElement</span> <span class="n">option</span> <span class="k">in</span> <span class="n">allOptions</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="s">"Value is: "</span> <span class="p">+</span> <span class="n">option</span><span class="p">.</span><span class="n">GetAttribute</span><span class="p">(</span><span class="s">"value"</span><span class="p">));</span>
    <span class="n">option</span><span class="p">.</span><span class="n">Click</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="nb">select</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:tag_name</span><span class="p">,</span> <span class="s2">"select"</span><span class="p">)</span>
<span class="n">all_options</span> <span class="o">=</span> <span class="nb">select</span><span class="o">.</span><span class="n">find_elements</span><span class="p">(</span><span class="ss">:tag_name</span><span class="p">,</span> <span class="s2">"option"</span><span class="p">)</span>
<span class="n">all_options</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">option</span><span class="o">|</span>
  <span class="nb">puts</span> <span class="s2">"Value is: "</span> <span class="o">+</span> <span class="n">option</span><span class="o">.</span><span class="n">attribute</span><span class="p">(</span><span class="s2">"value"</span><span class="p">)</span>
  <span class="n">option</span><span class="o">.</span><span class="n">click</span>
<span class="k">end</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">select</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_tag_name</span><span class="p">(</span><span class="s2">"select"</span><span class="p">)</span>
<span class="n">allOptions</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">find_elements_by_tag_name</span><span class="p">(</span><span class="s2">"option"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">option</span> <span class="ow">in</span> <span class="n">allOptions</span><span class="p">:</span>
    <span class="k">print</span> <span class="s2">"Value is: "</span> <span class="o">+</span> <span class="n">option</span><span class="o">.</span><span class="n">get_attribute</span><span class="p">(</span><span class="s2">"value"</span><span class="p">)</span>
    <span class="n">option</span><span class="o">.</span><span class="n">click</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$select</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_element</span><span class="p">(</span><span class="s">'select'</span><span class="p">,</span> <span class="s">'tag_name'</span><span class="p">);</span>
<span class="nv">@allOptions</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_child_element</span><span class="p">(</span><span class="nv">$select</span><span class="p">,</span> <span class="s">"option"</span><span class="p">,</span> <span class="s">'tag_name'</span><span class="p">);</span>
<span class="k">foreach</span> <span class="nv">$option</span> <span class="p">(</span><span class="nv">@allOptions</span><span class="p">){</span>
    <span class="k">print</span> <span class="s">"Value is: "</span><span class="o">.</span><span class="nv">$option</span><span class="o">-&gt;</span><span class="n">get_attribute</span><span class="p">(</span><span class="s">"value"</span><span class="p">)</span><span class="o">.</span><span class="s">"\n"</span><span class="p">;</span>
    <span class="nv">$option</span><span class="o">-&gt;</span><span class="n">click</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This will find the first “SELECT” element on the page, and cycle through each
of its OPTIONs in turn, printing out their values, and selecting each in turn.
As you will notice, this isn’t the most efficient way of dealing with SELECT
elements. WebDriver’s support classes include one called “Select”, which
provides useful methods for interacting with these.</p>
<p>这将在页面上找到第一个“SELECT”元素，并依次遍历每个OPTION，打印出它们的值，并依次选择它们。 你会注意到，这不是处理SELECT元素的最有效的方法。  WebDriver的支持类包括一个名为“Select”的类，它提供了与这些类交互的有用方法。</p>


<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">Select</span> <span class="n">select</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Select</span><span class="o">(</span><span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">tagName</span><span class="o">(</span><span class="s">"select"</span><span class="o">)));</span>
<span class="n">select</span><span class="o">.</span><span class="na">deselectAll</span><span class="o">();</span>
<span class="n">select</span><span class="o">.</span><span class="na">selectByVisibleText</span><span class="o">(</span><span class="s">"Edam"</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">SelectElement</span> <span class="k">select</span> <span class="p">=</span> <span class="k">new</span> <span class="n">SelectElement</span><span class="p">(</span><span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">TagName</span><span class="p">(</span><span class="s">"select"</span><span class="p">)));</span>
<span class="k">select</span><span class="p">.</span><span class="n">DeselectAll</span><span class="p">();</span>
<span class="k">select</span><span class="p">.</span><span class="n">SelectByText</span><span class="p">(</span><span class="s">"Edam"</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="c1"># available since 2.14</span>
    <span class="nb">select</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Support</span><span class="o">::</span><span class="no">Select</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:tag_name</span><span class="p">,</span> <span class="s2">"select"</span><span class="p">))</span>
    <span class="nb">select</span><span class="o">.</span><span class="n">deselect_all</span><span class="p">()</span>
    <span class="nb">select</span><span class="o">.</span><span class="n">select_by</span><span class="p">(</span><span class="ss">:text</span><span class="p">,</span> <span class="s2">"Edam"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="c1"># available since 2.12</span>
<span class="kn">from</span> <span class="nn">selenium.webdriver.support.ui</span> <span class="kn">import</span> <span class="n">Select</span>
<span class="n">select</span> <span class="o">=</span> <span class="n">Select</span><span class="p">(</span><span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_tag_name</span><span class="p">(</span><span class="s2">"select"</span><span class="p">))</span>
<span class="n">select</span><span class="o">.</span><span class="n">deselect_all</span><span class="p">()</span>
<span class="n">select</span><span class="o">.</span><span class="n">select_by_visible_text</span><span class="p">(</span><span class="s2">"Edam"</span><span class="p">)</span>
</pre></div>
</div>
<p>This will deselect all OPTIONs from the first SELECT on the page, and then
select the OPTION with the displayed text of “Edam”.</p>
<p>这将从页面上的第一个SELECT中取消选择所有OPTION，然后选择显示文本“Edam”的OPTION。</p>

<p>Once you’ve finished filling out the form, you probably want to submit it. One
way to do this would be to find the “submit” button and click it:</p>
<p>填完表格后，您可能要提交。 一种方法是找到“提交”按钮并单击它</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">id</span><span class="o">(</span><span class="s">"submit"</span><span class="o">)).</span><span class="na">click</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">Id</span><span class="p">(</span><span class="s">"submit"</span><span class="p">)).</span><span class="n">Click</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:id</span><span class="p">,</span> <span class="s2">"submit"</span><span class="p">)</span><span class="o">.</span><span class="n">click</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_id</span><span class="p">(</span><span class="s2">"submit"</span><span class="p">)</span><span class="o">.</span><span class="n">click</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">find_element</span><span class="p">(</span><span class="s">'submit'</span><span class="p">,</span><span class="s">'id'</span><span class="p">)</span><span class="o">-&gt;</span><span class="n">click</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="nx">driver</span><span class="p">.</span><span class="nx">findElement</span><span class="p">(</span><span class="nx">By</span><span class="p">.</span><span class="nx">id</span><span class="p">(</span><span class="s1">'submit'</span><span class="p">).</span><span class="nx">click</span><span class="p">();</span>
</pre></div>
</div>
<p>Alternatively, WebDriver has the convenience method “submit” on every element.
If you call this on an element within a form, WebDriver will walk up the DOM
until it finds the enclosing form and then calls submit on that. If the
element isn’t in a form, then the <code class="docutils literal"><span class="pre">NoSuchElementException</span></code> will be thrown:</p>
<p>或者，WebDriver在每个元素上都有“提交”的便利方法。 如果你在表单中的元素上调用它，WebDriver会遍历DOM，直到找到包含的表单，然后调用它。 如果元素不在表单中，则会抛出NoSuchElementException异常</p>


<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">element</span><span class="o">.</span><span class="na">submit</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span><span class="p">.</span><span class="n">Submit</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span><span class="o">.</span><span class="n">submit</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span><span class="o">.</span><span class="n">submit</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$element</span><span class="o">-&gt;</span><span class="n">submit</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="nx">element</span><span class="p">.</span><span class="nx">submit</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="section" id="moving-between-windows-and-frames">
<h3>Moving Between Windows and Frames<a class="headerlink" href="#moving-between-windows-and-frames" title="Permalink to this headline">¶</a></h3>
<p>Some web applications have many frames or multiple windows. WebDriver supports
moving between named windows using the “switchTo” method:</p>
<p>一些Web应用程序有许多框架或多个窗口。  WebDriver支持使用“switchTo”方法在命名窗口之间移动</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="na">switchTo</span><span class="o">().</span><span class="na">window</span><span class="o">(</span><span class="s">"windowName"</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="p">.</span><span class="n">SwitchTo</span><span class="p">().</span><span class="n">Window</span><span class="p">(</span><span class="s">"windowName"</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">switch_to</span><span class="o">.</span><span class="n">window</span><span class="p">(</span><span class="s2">"windowName"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">switch_to</span><span class="o">.</span><span class="n">window</span><span class="p">(</span><span class="s2">"windowName"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">switch_to_window</span><span class="p">(</span><span class="s">"windowName"</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="nx">driver</span><span class="p">.</span><span class="nx">switchTo</span><span class="p">().</span><span class="nb">window</span><span class="p">(</span><span class="s1">'windowName'</span><span class="p">);</span>
</pre></div>
</div>
<p>All calls to <code class="docutils literal"><span class="pre">driver</span></code> will now be interpreted as being directed to the
particular window. But how do you know the window’s name? Take a look at the
javascript or link that opened it:</p>
<p>所有的驱动程序调用现在将被解释为指向特定的窗口。 但你怎么知道这个窗口的名字？ 看看打开它的JavaScript或链接</p>

<div class="highlight-html"><div class="highlight"><pre><span></span><span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"somewhere.html"</span> <span class="na">target</span><span class="o">=</span><span class="s">"windowName"</span><span class="p">&gt;</span>Click here to open a new window<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>Alternatively, you can pass a “window handle” to the “switchTo().window()”
method. Knowing this, it’s possible to iterate over every open window like so:</p>
<p>或者，您可以将“窗口句柄”传递给“switchTo（）。window（）”方法。 知道这一点，就可以迭代每一个打开的窗口</p>


<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="o">(</span><span class="n">String</span> <span class="n">handle</span> <span class="o">:</span> <span class="n">driver</span><span class="o">.</span><span class="na">getWindowHandles</span><span class="o">())</span> <span class="o">{</span>
    <span class="n">driver</span><span class="o">.</span><span class="na">switchTo</span><span class="o">().</span><span class="na">window</span><span class="o">(</span><span class="n">handle</span><span class="o">);</span>
<span class="o">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="k">foreach</span> <span class="p">(</span><span class="kt">string</span> <span class="n">handle</span> <span class="k">in</span> <span class="n">driver</span><span class="p">.</span><span class="n">WindowHandles</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">driver</span><span class="p">.</span><span class="n">SwitchTo</span><span class="p">().</span><span class="n">Window</span><span class="p">(</span><span class="n">handle</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">window_handles</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">handle</span><span class="o">|</span>
    <span class="n">driver</span><span class="o">.</span><span class="n">switch_to</span><span class="o">.</span><span class="n">window</span> <span class="n">handle</span>
<span class="k">end</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">handle</span> <span class="ow">in</span> <span class="n">driver</span><span class="o">.</span><span class="n">window_handles</span><span class="p">:</span>
    <span class="n">driver</span><span class="o">.</span><span class="n">switch_to</span><span class="o">.</span><span class="n">window</span><span class="p">(</span><span class="n">handle</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$windows</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">get_window_handles</span><span class="p">()</span>
<span class="k">foreach</span> <span class="nv">$window</span> <span class="p">(</span><span class="nv">@$windows</span><span class="p">){</span>
    <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">switch_to_window</span><span class="p">(</span><span class="nv">$window</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You can also switch from frame to frame (or into iframes):</p>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="na">switchTo</span><span class="o">().</span><span class="na">frame</span><span class="o">(</span><span class="s">"frameName"</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="p">.</span><span class="n">SwitchTo</span><span class="p">().</span><span class="n">Frame</span><span class="p">(</span><span class="s">"frameName"</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">switch_to</span><span class="o">.</span><span class="n">frame</span> <span class="s2">"frameName"</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">switch_to</span><span class="o">.</span><span class="n">frame</span><span class="p">(</span><span class="s2">"frameName"</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">switch_to_frame</span><span class="p">(</span><span class="s">'frameName'</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="nx">driver</span><span class="p">.</span><span class="nx">switchTo</span><span class="p">().</span><span class="nx">frame</span><span class="p">(</span><span class="s1">'frameName'</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="popup-dialogs">
<h3>Popup Dialogs<a class="headerlink" href="#popup-dialogs" title="Permalink to this headline">¶</a></h3>
<p>Starting with Selenium 2.0 beta 1, there is built in support for handling popup
dialog boxes. After you’ve triggered an action that opens a
popup, you can access the alert with the following:</p>
<p>从Selenium 2.0 beta 1开始，支持处理弹出对话框。 触发了一个打开弹出窗口的操作之后，可以使用以下命令访问该警报</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">Alert</span> <span class="n">alert</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">switchTo</span><span class="o">().</span><span class="na">alert</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">Alert</span> <span class="n">alert</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">SwitchTo</span><span class="p">().</span><span class="n">Alert</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">alert</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">switch_to</span><span class="o">.</span><span class="n">alert</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">alert</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">switch_to</span><span class="o">.</span><span class="n">alert</span>
<span class="c1"># usage: alert.dismiss(), etc.</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">get_alert_text</span><span class="p">();</span>
<span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">accept_alert</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="kd">var</span> <span class="nx">alert</span> <span class="o">=</span> <span class="nx">driver</span><span class="p">.</span><span class="nx">switchTo</span><span class="p">().</span><span class="nx">alert</span><span class="p">();</span>
<span class="nx">alert</span><span class="p">.</span><span class="nx">accept</span><span class="p">();</span>
</pre></div>
</div>
<p>This will return the currently open alert object. With this object you can now accept,
dismiss, read its contents or even type into a prompt. This interface works equally
well on alerts, confirms, and prompts. Refer to the <a class="reference external" href="https://seleniumhq.github.io/selenium/docs/api/java/index.html">JavaDocs</a>
or <a class="reference external" href="https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Alert.html">RubyDocs</a> for more information.</p>
<p>这将返回当前打开的警报对象。 有了这个对象，你现在可以接受，解雇，阅读其内容，甚至键入提示。 这个界面在警报，确认和提示方面同样适用。 有关更多信息，请参阅JavaDocs或RubyDocs。</p>

</div>
<div class="section" id="navigation-history-and-location">
<h3>Navigation: History and Location<a class="headerlink" href="#navigation-history-and-location" title="Permalink to this headline">¶</a></h3>
<p>Earlier, we covered navigating to a page using the “get” command (
<code class="docutils literal"><span class="pre">driver.get("http://www.example.com")</span></code> or <code class="docutils literal"><span class="pre">driver.Url="http://www.example.com"</span></code> in C#).
As you’ve seen, WebDriver has a number of smaller, task-focused interfaces, and navigation is a useful task.
Because loading a page is such a fundamental requirement, the method to do this
lives on the main WebDriver interface, but it’s simply a synonym to:</p>
<p>早些时候，我们使用C＃中的“get”命令（driver.get（“http//www.example.com”）或driver.Url =“http//www.example.com”）导航到页面。  。 正如你所看到的，WebDriver有一些小的，以任务为中心的界面，导航是一个有用的任务。 因为加载一个页面是一个基本的要求，所以要做到这一点的方法主要在WebDriver接口上，但它只是一个同义词</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="na">navigate</span><span class="o">().</span><span class="na">to</span><span class="o">(</span><span class="s">"http://www.example.com"</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="p">.</span><span class="n">Navigate</span><span class="p">().</span><span class="n">GoToUrl</span><span class="p">(</span><span class="s">"http://www.example.com"</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">navigate</span><span class="o">.</span><span class="n">to</span> <span class="s2">"http://www.example.com"</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"http://www.example.com"</span><span class="p">)</span>  <span class="c1"># python doesn't have driver.navigate</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">navigate</span><span class="p">(</span><span class="s">'http://www.example.com'</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="nx">driver</span><span class="p">.</span><span class="nx">navigate</span><span class="p">().</span><span class="nx">to</span><span class="p">(</span><span class="s1">'http://www.example.com'</span><span class="p">);</span>
</pre></div>
</div>
<p>To reiterate: “<code class="docutils literal"><span class="pre">navigate().to()</span></code>” and “<code class="docutils literal"><span class="pre">get()</span></code>” do exactly the same thing.
One’s just a lot easier to type than the other!</p>
<p>重申：“navigate（）。to（）”和“get（）”完全一样。 一个比另一个更容易打字！</p>

<p>The “navigate” interface also exposes the ability to move backwards and forwards in your browser’s history:</p>
<p>“导航”界面也暴露了在浏览器历史中前后移动的能力</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="na">navigate</span><span class="o">().</span><span class="na">forward</span><span class="o">();</span>
<span class="n">driver</span><span class="o">.</span><span class="na">navigate</span><span class="o">().</span><span class="na">back</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="p">.</span><span class="n">Navigate</span><span class="p">().</span><span class="n">Forward</span><span class="p">();</span>
<span class="n">driver</span><span class="p">.</span><span class="n">Navigate</span><span class="p">().</span><span class="n">Back</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">navigate</span><span class="o">.</span><span class="n">forward</span>
<span class="n">driver</span><span class="o">.</span><span class="n">navigate</span><span class="o">.</span><span class="n">back</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span><span class="o">.</span><span class="n">forward</span><span class="p">()</span>
<span class="n">driver</span><span class="o">.</span><span class="n">back</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span><span class="nx">driver</span><span class="p">.</span><span class="nx">navigate</span><span class="p">().</span><span class="nx">forward</span><span class="p">();</span>
<span class="nx">driver</span><span class="p">.</span><span class="nx">navigate</span><span class="p">().</span><span class="nx">back</span><span class="p">();</span>
</pre></div>
</div>
<p>Please be aware that this functionality depends entirely on the underlying
browser. It’s just possible that something unexpected may happen when you call
these methods if you’re used to the behaviour of one browser over another.</p>
<p>请注意，这个功能完全取决于底层的浏览器。 如果您习惯了一个浏览器的行为而另一个浏览器的行为，那么当您调用这些方法时，可能会发生意想不到的情况。</p>

</div>
<div class="section" id="cookies">
<h3>Cookies<a class="headerlink" href="#cookies" title="Permalink to this headline">¶</a></h3>
<p>Before we leave these next steps, you may be interested in understanding how to
use cookies. First of all, you need to be on the domain that the cookie will be
valid for. If you are trying to preset cookies before
you start interacting with a site and your homepage is large / takes a while to load
an alternative is to find a smaller page on the site (typically the 404 page is small,
e.g. <a class="reference external" href="http://example.com/some404page">http://example.com/some404page</a>).</p>
<p>在我们离开这些下一步之前，您可能有兴趣了解如何使用cookie。 首先，你需要在cookie的有效域名上。 如果您在开始与网站交互之前尝试预设Cookie，而且您的主页很大/需要一段时间才能加载替代方法是在网站上查找较小的页面（通常404页面很小，例如http：//示例 的.com / some404page）。</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="c1">// Go to the correct domain</span>
<span class="n">driver</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="s">"http://www.example.com"</span><span class="o">);</span>

<span class="c1">// Now set the cookie. This one's valid for the entire domain</span>
<span class="n">Cookie</span> <span class="n">cookie</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Cookie</span><span class="o">(</span><span class="s">"key"</span><span class="o">,</span> <span class="s">"value"</span><span class="o">);</span>
<span class="n">driver</span><span class="o">.</span><span class="na">manage</span><span class="o">().</span><span class="na">addCookie</span><span class="o">(</span><span class="n">cookie</span><span class="o">);</span>

<span class="c1">// And now output all the available cookies for the current URL</span>
<span class="n">Set</span><span class="o">&lt;</span><span class="n">Cookie</span><span class="o">&gt;</span> <span class="n">allCookies</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">manage</span><span class="o">().</span><span class="na">getCookies</span><span class="o">();</span>
<span class="k">for</span> <span class="o">(</span><span class="n">Cookie</span> <span class="n">loadedCookie</span> <span class="o">:</span> <span class="n">allCookies</span><span class="o">)</span> <span class="o">{</span>
    <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">String</span><span class="o">.</span><span class="na">format</span><span class="o">(</span><span class="s">"%s -&gt; %s"</span><span class="o">,</span> <span class="n">loadedCookie</span><span class="o">.</span><span class="na">getName</span><span class="o">(),</span> <span class="n">loadedCookie</span><span class="o">.</span><span class="na">getValue</span><span class="o">()));</span>
<span class="o">}</span>

<span class="c1">// You can delete cookies in 3 ways</span>
<span class="c1">// By name</span>
<span class="n">driver</span><span class="o">.</span><span class="na">manage</span><span class="o">().</span><span class="na">deleteCookieNamed</span><span class="o">(</span><span class="s">"CookieName"</span><span class="o">);</span>
<span class="c1">// By Cookie</span>
<span class="n">driver</span><span class="o">.</span><span class="na">manage</span><span class="o">().</span><span class="na">deleteCookie</span><span class="o">(</span><span class="n">loadedCookie</span><span class="o">);</span>
<span class="c1">// Or all of them</span>
<span class="n">driver</span><span class="o">.</span><span class="na">manage</span><span class="o">().</span><span class="na">deleteAllCookies</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="c1"># Go to the correct domain</span>
<span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"http://www.example.com"</span><span class="p">)</span>

<span class="c1"># Now set the cookie. Here's one for the entire domain</span>
<span class="c1"># the cookie name here is 'key' and its value is 'value'</span>
<span class="n">driver</span><span class="o">.</span><span class="n">add_cookie</span><span class="p">({</span><span class="s1">'name'</span><span class="p">:</span><span class="s1">'key'</span><span class="p">,</span> <span class="s1">'value'</span><span class="p">:</span><span class="s1">'value'</span><span class="p">,</span> <span class="s1">'path'</span><span class="p">:</span><span class="s1">'/'</span><span class="p">})</span>
<span class="c1"># additional keys that can be passed in are:</span>
<span class="c1"># 'domain' -&gt; String,</span>
<span class="c1"># 'secure' -&gt; Boolean,</span>
<span class="c1"># 'expiry' -&gt; Milliseconds since the Epoch it should expire.</span>

<span class="c1"># And now output all the available cookies for the current URL</span>
<span class="k">for</span> <span class="n">cookie</span> <span class="ow">in</span> <span class="n">driver</span><span class="o">.</span><span class="n">get_cookies</span><span class="p">():</span>
    <span class="k">print</span> <span class="s2">"</span><span class="si">%s</span><span class="s2"> -&gt; </span><span class="si">%s</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">cookie</span><span class="p">[</span><span class="s1">'name'</span><span class="p">],</span> <span class="n">cookie</span><span class="p">[</span><span class="s1">'value'</span><span class="p">])</span>

<span class="c1"># You can delete cookies in 2 ways</span>
<span class="c1"># By name</span>
<span class="n">driver</span><span class="o">.</span><span class="n">delete_cookie</span><span class="p">(</span><span class="s2">"CookieName"</span><span class="p">)</span>
<span class="c1"># Or all of them</span>
<span class="n">driver</span><span class="o">.</span><span class="n">delete_all_cookies</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="c1"># Go to the correct domain</span>
<span class="n">driver</span><span class="o">.</span><span class="n">get</span> <span class="s2">"http://www.example.com"</span>

<span class="c1"># Now set the cookie. Here's one for the entire domain</span>
<span class="c1"># the cookie name here is 'key' and its value is 'value'</span>
<span class="n">driver</span><span class="o">.</span><span class="n">manage</span><span class="o">.</span><span class="n">add_cookie</span><span class="p">(</span><span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s1">'key'</span><span class="p">,</span> <span class="ss">:value</span> <span class="o">=&gt;</span> <span class="s1">'value'</span><span class="p">)</span>
<span class="c1"># additional keys that can be passed in are:</span>
<span class="c1"># :path =&gt; String, :secure -&gt; Boolean, :expires -&gt; Time, DateTime, or seconds since epoch</span>

<span class="c1"># And now output all the available cookies for the current URL</span>
<span class="n">driver</span><span class="o">.</span><span class="n">manage</span><span class="o">.</span><span class="n">all_cookies</span><span class="o">.</span><span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">cookie</span><span class="o">|</span>
    <span class="nb">puts</span> <span class="s2">"</span><span class="si">#{</span><span class="n">cookie</span><span class="o">[</span><span class="ss">:name</span><span class="o">]</span><span class="si">}</span><span class="s2"> =&gt; </span><span class="si">#{</span><span class="n">cookie</span><span class="o">[</span><span class="ss">:value</span><span class="o">]</span><span class="si">}</span><span class="s2">"</span>
<span class="p">}</span>

<span class="c1"># You can delete cookies in 2 ways</span>
<span class="c1"># By name</span>
<span class="n">driver</span><span class="o">.</span><span class="n">manage</span><span class="o">.</span><span class="n">delete_cookie</span> <span class="s2">"CookieName"</span>
<span class="c1"># Or all of them</span>
<span class="n">driver</span><span class="o">.</span><span class="n">manage</span><span class="o">.</span><span class="n">delete_all_cookies</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="c1"># Go to the correct domain</span>
<span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">get</span><span class="p">(</span><span class="s">"http://www.example.com"</span><span class="p">);</span>

<span class="c1"># Now set the cookie. Here's one for the entire domain</span>
<span class="c1"># the cookie name here is 'key' and its value is 'value'</span>
<span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">add_cookie</span><span class="p">(</span><span class="s">'key'</span><span class="p">,</span> <span class="s">'value'</span><span class="p">,</span> <span class="s">'/'</span><span class="p">,</span> <span class="s">'example.com'</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
<span class="c1"># additional required inputs are path and domain</span>
<span class="c1"># the final input secure is an optional boolean</span>

<span class="c1"># And now output all the available cookies for the current URL</span>
<span class="k">my</span> <span class="nv">$cookies_ref</span> <span class="o">=</span> <span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">get_all_cookies</span><span class="p">();</span> <span class="c1"># Returns reference to AoH</span>
<span class="k">for</span> <span class="nv">$cookie_ref</span> <span class="p">(</span><span class="nv">@</span><span class="p">{</span><span class="nv">$cookies_ref</span><span class="p">})</span> <span class="p">{</span>
    <span class="nb">printf</span> <span class="s">"%s =&gt; %s\n"</span><span class="p">,</span> <span class="nv">$cookie_ref</span><span class="o">-&gt;</span><span class="p">{</span><span class="n">name</span><span class="p">},</span> <span class="nv">$cookie_ref</span><span class="o">-&gt;</span><span class="p">{</span><span class="n">value</span><span class="p">};</span>
<span class="p">}</span>

<span class="c1"># You can delete cookies in 2 ways</span>
<span class="c1"># By name</span>
<span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">delete_cookie_named</span><span class="p">(</span><span class="s">"key"</span><span class="p">);</span>
<span class="c1"># Or all of them</span>
<span class="nv">$driver</span><span class="o">-&gt;</span><span class="n">delete_all_cookies</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-javascript" value="javascript" src="images/icons/javascript.png" style="padding: 3px;"><div class="highlight-javascript" style="display: none;"><div class="highlight"><pre><span></span># Go to the correct domain
driver.get('http://www.example.com');

# Now set the basic cookie. Here's one for the entire domain
# the cookie name here is 'key' and its value is 'value'
driver.manage().addCookie({name: 'cookie-1', value: 'cookieValue'});

# And now output all the available cookies for the current URL
driver.manage().getCookies().then( (loadedCookies) =&gt;{
    for (let cookie in loadedCookies) {
    console.log('printing Cookies loaded : '+cookie);
    }
    });
# You can delete cookies in 2 ways
# By name
driver.manage().deleteCookie('cookie-1');
# Or all of them
driver.manage().deleteAllCookies();
</pre></div>
</div>
</div>
<div class="section" id="changing-the-user-agent">
<h3>Changing the User Agent<a class="headerlink" href="#changing-the-user-agent" title="Permalink to this headline">¶</a></h3>
<p>This is easy with the <a class="reference internal" href="#firefox-driver">Firefox Driver</a>:</p>
<p>使用Firefox驱动程序很容易</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">FirefoxProfile</span> <span class="n">profile</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxProfile</span><span class="o">();</span>
<span class="n">profile</span><span class="o">.</span><span class="na">addAdditionalPreference</span><span class="o">(</span><span class="s">"general.useragent.override"</span><span class="o">,</span> <span class="s">"some UA string"</span><span class="o">);</span>
<span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">(</span><span class="n">profile</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="kt">var</span> <span class="n">profile</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxProfile</span><span class="p">();</span>
<span class="n">profile</span><span class="p">.</span><span class="n">SetPreference</span><span class="p">(</span><span class="s">"general.useragent.override"</span><span class="p">,</span> <span class="s">"some UA string"</span><span class="p">);</span>
<span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="p">(</span><span class="n">profile</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">profile</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Firefox</span><span class="o">::</span><span class="no">Profile</span><span class="o">.</span><span class="n">new</span>
<span class="n">profile</span><span class="o">[</span><span class="s1">'general.useragent.override'</span><span class="o">]</span> <span class="o">=</span> <span class="s2">"some UA string"</span>
<span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:firefox</span><span class="p">,</span> <span class="ss">:profile</span> <span class="o">=&gt;</span> <span class="n">profile</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">profile</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">FirefoxProfile</span><span class="p">()</span>
<span class="n">profile</span><span class="o">.</span><span class="n">set_preference</span><span class="p">(</span><span class="s2">"general.useragent.override"</span><span class="p">,</span> <span class="s2">"some UA string"</span><span class="p">)</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Firefox</span><span class="p">(</span><span class="n">profile</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="k">use</span> <span class="nn">Selenium::Remote::Driver::Firefox::Profile</span><span class="p">;</span>
<span class="k">my</span> <span class="nv">$profile</span> <span class="o">=</span> <span class="nn">Selenium::Remote::Driver::Firefox::Profile</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">;</span>
<span class="nv">$profile</span><span class="o">-&gt;</span><span class="n">set_preference</span><span class="p">(</span><span class="s">'general.useragent.overide'</span> <span class="o">=&gt;</span> <span class="s">"some UA string"</span><span class="p">);</span>
<span class="k">my</span> <span class="nv">$driver</span> <span class="o">=</span> <span class="nn">Selenium::Remote::Driver</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">(</span><span class="s">'firefox_profile'</span> <span class="o">=&gt;</span> <span class="nv">$profile</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="drag-and-drop">
<h3>Drag And Drop<a class="headerlink" href="#drag-and-drop" title="Permalink to this headline">¶</a></h3>
<p>Here’s an example of using the Actions class to perform a drag and drop.
Native events are required to be enabled.</p>
<p>以下是使用Actions类执行拖放操作的示例。 本机事件需要启用。</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebElement</span> <span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">name</span><span class="o">(</span><span class="s">"source"</span><span class="o">));</span>
<span class="n">WebElement</span> <span class="n">target</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="na">findElement</span><span class="o">(</span><span class="n">By</span><span class="o">.</span><span class="na">name</span><span class="o">(</span><span class="s">"target"</span><span class="o">));</span>

<span class="o">(</span><span class="k">new</span> <span class="n">Actions</span><span class="o">(</span><span class="n">driver</span><span class="o">)).</span><span class="na">dragAndDrop</span><span class="o">(</span><span class="n">element</span><span class="o">,</span> <span class="n">target</span><span class="o">).</span><span class="na">perform</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebElement</span> <span class="n">element</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">Name</span><span class="p">(</span><span class="s">"source"</span><span class="p">));</span>
<span class="n">IWebElement</span> <span class="n">target</span> <span class="p">=</span> <span class="n">driver</span><span class="p">.</span><span class="n">FindElement</span><span class="p">(</span><span class="n">By</span><span class="p">.</span><span class="n">Name</span><span class="p">(</span><span class="s">"target"</span><span class="p">));</span>
<span class="p">(</span><span class="k">new</span> <span class="n">Actions</span><span class="p">(</span><span class="n">driver</span><span class="p">)).</span><span class="n">DragAndDrop</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="n">target</span><span class="p">).</span><span class="n">Perform</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s1">'source'</span><span class="p">)</span>
<span class="n">target</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element</span><span class="p">(</span><span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s1">'target'</span><span class="p">)</span>

<span class="n">driver</span><span class="o">.</span><span class="n">action</span><span class="o">.</span><span class="n">drag_and_drop</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="n">target</span><span class="p">)</span><span class="o">.</span><span class="n">perform</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">selenium.webdriver.common.action_chains</span> <span class="kn">import</span> <span class="n">ActionChains</span>
<span class="n">element</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s2">"source"</span><span class="p">)</span>
<span class="n">target</span> <span class="o">=</span>  <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_name</span><span class="p">(</span><span class="s2">"target"</span><span class="p">)</span>

<span class="n">ActionChains</span><span class="p">(</span><span class="n">driver</span><span class="p">)</span><span class="o">.</span><span class="n">drag_and_drop</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="n">target</span><span class="p">)</span><span class="o">.</span><span class="n">perform</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$element</span><span class="o">-&gt;</span><span class="n">drag</span><span class="p">(</span><span class="mi">216</span><span class="p">,</span><span class="mi">158</span><span class="p">);</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="driver-specifics-and-tradeoffs">
<h2>Driver Specifics and Tradeoffs<a class="headerlink" href="#driver-specifics-and-tradeoffs" title="Permalink to this headline">¶</a></h2>
</div>
<div class="section" id="selenium-webdriver-s-drivers">
<h2>Selenium-WebDriver’s Drivers<a class="headerlink" href="#selenium-webdriver-s-drivers" title="Permalink to this headline">¶</a></h2>
<p>WebDriver is the name of the key interface against which tests should be
written, but there are several implementations. These include:</p>
<p>WebDriver是应该编写测试的关键接口的名称，但有几个实现。 这些包括</p>


<div class="section" id="htmlunit-driver">
<h3>HtmlUnit Driver<a class="headerlink" href="#htmlunit-driver" title="Permalink to this headline">¶</a></h3>
<p>This is currently the fastest and most lightweight implementation of WebDriver.
As the name suggests, this is based on HtmlUnit. HtmlUnit is a java based implementation
of a WebBrowser without a GUI. For any language binding (other than java) the
Selenium Server is required to use this driver.</p>
<p>这是目前WebDriver最快和最轻量级的实现。 顾名思义，这是基于HtmlUnit的。  HtmlUnit是一个没有GUI的WebBrowser的基于Java的实现。 对于任何语言绑定（除了Java）Selenium服务器需要使用此驱动程序。</p>

<div class="section" id="usage">
<h4>Usage<a class="headerlink" href="#usage" title="Permalink to this headline">¶</a></h4>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HtmlUnitDriver</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">RemoteWebDriver</span><span class="p">(</span><span class="k">new</span> <span class="n">Uri</span><span class="p">(</span><span class="s">"http://127.0.0.1:4444/wd/hub"</span><span class="p">),</span>
                                        <span class="n">DesiredCapabilities</span><span class="p">.</span><span class="n">HtmlUnit</span><span class="p">());</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Remote</span><span class="p">(</span><span class="s2">"http://localhost:4444/wd/hub"</span><span class="p">,</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">DesiredCapabilities</span><span class="o">.</span><span class="n">HTMLUNIT</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:remote</span><span class="p">,</span> <span class="ss">:url</span> <span class="o">=&gt;</span> <span class="s2">"http://localhost:4444/wd/hub"</span><span class="p">,</span> <span class="ss">:desired_capabilities</span> <span class="o">=&gt;</span> <span class="ss">:htmlunit</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="k">my</span> <span class="nv">$driver</span> <span class="o">=</span> <span class="nn">Selenium::Remote::Driver</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">(</span><span class="n">browser_name</span> <span class="o">=&gt;</span> <span class="s">'htmlunit'</span><span class="p">,</span> <span class="n">remote_server_addr</span> <span class="o">=&gt;</span> <span class="s">'localhost'</span><span class="p">,</span> <span class="n">port</span> <span class="o">=&gt;</span> <span class="s">'4444'</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="pros">
<h4>Pros<a class="headerlink" href="#pros" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>Fastest implementation of WebDriver <span class="subA">最快的WebDriver实现</span></li>
<li>A pure Java solution and so it is platform independent. <span class="subA">一个纯粹的Java解决方案，因此它是平台独立的。</span> </li>
<li>Supports JavaScript</li>
</ul>
</div>
<div class="section" id="cons">
<h4>Cons<a class="headerlink" href="#cons" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>Emulates other browsers’ JavaScript behaviour (see below) <span class="subA">模拟其他浏览器的JavaScript行为（见下文）</span></li>
</ul>
</div>
<div class="section" id="javascript-in-the-htmlunit-driver">
<h4>JavaScript in the HtmlUnit Driver<a class="headerlink" href="#javascript-in-the-htmlunit-driver" title="Permalink to this headline">¶</a></h4>
<p>None of the popular browsers uses the JavaScript engine used by HtmlUnit
(Rhino). If you test JavaScript using HtmlUnit the results may differ
significantly from those browsers.</p>
<p>没有流行的浏览器使用HtmlUnit（Rhino）使用的JavaScript引擎。 如果使用HtmlUnit测试JavaScript，结果可能会与这些浏览器显着不同。</p>

<p>When we say “JavaScript” we actually mean “JavaScript and the DOM”. Although
the DOM is defined by the W3C each browser has its own quirks and differences
in their implementation of the DOM and in how JavaScript interacts with it.
HtmlUnit has an impressively complete implementation of the DOM and has good
support for using JavaScript, but it is no different from any other
browser: it has its own quirks and differences from both the W3C standard and
the DOM implementations of the major browsers, despite its ability to mimic
other browsers.</p>
<p>当我们说“JavaScript”时，我们实际上是指“JavaScript和DOM”。 虽然DOM是由W3C定义的，但每个浏览器都有自己的怪癖和差异，在DOM的实现以及JavaScript如何与它交互。  HtmlUnit有一个令人印象深刻的完整的DOM实现，并且对使用JavaScript有很好的支持，但是与其他任何浏览器没有什么不同：它与W3C标准和主要浏览器的DOM实现都有自己的差异 模仿其他浏览器的能力。</p>

<p>With WebDriver, we had to make a choice; do we enable HtmlUnit’s JavaScript
capabilities and run the risk of teams running into problems that only manifest
themselves there, or do we leave JavaScript disabled, knowing that there are
more and more sites that rely on JavaScript? We took the conservative approach,
and by default have disabled support when we use HtmlUnit. With each release of
both WebDriver and HtmlUnit, we reassess this decision: we hope to enable
JavaScript by default on the HtmlUnit at some point.</p>
<p>使用WebDriver，我们必须做出选择; 我们是否启用HtmlUnit的JavaScript功能，并冒着团队遇到问题的风险，或者我们是否禁用了JavaScript，知道有越来越多的站点依赖于JavaScript？ 我们采取了保守的方法，并且默认情况下已经禁用了我们使用HtmlUnit的支持。 对于WebDriver和HtmlUnit的每个版本，我们重新评估这个决定：我们希望在某个时候在HtmlUnit上默认启用JavaScript。</p>


</div>
<div class="section" id="enabling-javascript">
<h4>Enabling JavaScript<a class="headerlink" href="#enabling-javascript" title="Permalink to this headline">¶</a></h4>
<p>If you can’t wait, enabling JavaScript support is very easy:</p>
<p>如果您不能等待，启用JavaScript支持是非常容易的</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">HtmlUnitDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HtmlUnitDriver</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">WebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">RemoteWebDriver</span><span class="p">(</span><span class="k">new</span> <span class="n">Uri</span><span class="p">(</span><span class="s">"http://127.0.0.1:4444/wd/hub"</span><span class="p">),</span>
                       <span class="n">DesiredCapabilities</span><span class="p">.</span><span class="n">HtmlUnitWithJavaScript</span><span class="p">());</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">caps</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Remote</span><span class="o">::</span><span class="no">Capabilities</span><span class="o">.</span><span class="n">htmlunit</span><span class="p">(</span><span class="ss">:javascript_enabled</span> <span class="o">=&gt;</span> <span class="kp">true</span><span class="p">)</span>
<span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:remote</span><span class="p">,</span> <span class="ss">:url</span> <span class="o">=&gt;</span> <span class="s2">"http://localhost:4444/wd/hub"</span><span class="p">,</span> <span class="ss">:desired_capabilities</span> <span class="o">=&gt;</span> <span class="n">caps</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Remote</span><span class="p">(</span><span class="s2">"http://localhost:4444/wd/hub"</span><span class="p">,</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">DesiredCapabilities</span><span class="o">.</span><span class="n">HTMLUNITWITHJS</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="nv">$driver</span> <span class="o">=</span> <span class="k">new</span> <span class="nn">Selenium::Remote::</span><span class="n">Driver</span><span class="p">(</span> <span class="n">browser_name</span><span class="o">=&gt;</span><span class="s">'firefox'</span><span class="p">,</span> <span class="n">port</span><span class="o">=&gt;</span> <span class="mi">4444</span><span class="p">,</span> <span class="n">version</span><span class="o">=&gt;</span><span class="s">''</span><span class="p">,</span> <span class="n">platform</span><span class="o">=&gt;</span><span class="s">'LINUX'</span><span class="p">,</span> <span class="n">javascript</span><span class="o">=&gt;</span><span class="mi">1</span><span class="p">,</span> <span class="n">auto_close</span><span class="o">=&gt;</span><span class="mi">1</span><span class="p">);</span>
</pre></div>
</div>
<p>This will cause the HtmlUnit Driver to emulate Firefox 3.6’s JavaScript
handling by default.</p>
<p>这将导致HtmlUnit驱动程序默认模拟Firefox 3.6的JavaScript处理。</p>

</div>
</div>
<div class="section" id="firefox-driver">
<h3>Firefox Driver<a class="headerlink" href="#firefox-driver" title="Permalink to this headline">¶</a></h3>
<p>Controls the <a class="reference external" href="http://getfirefox.com">Firefox</a> browser using a Firefox plugin.
The Firefox Profile that is used is stripped down from what is installed on the
machine to only include the Selenium WebDriver.xpi (plugin). A few settings are
also changed by default (<a class="reference external" href="https://github.com/SeleniumHQ/selenium/blob/master/javascript/firefox-driver/webdriver.json">see the source to see which ones</a>)
Firefox Driver is capable of being run and is tested on Windows, Mac, Linux.
Currently on versions 3.6, 10, latest - 1, latest</p>
<p>使用Firefox插件控制Firefox浏览器。 所使用的Firefox配置文件从机器上安装的内容中剥离，只包含Selenium WebDriver.xpi（插件）。 一些设置也是默认改变的（请参阅源代码以查看哪些设置）Firefox驱动程序可以运行并在Windows，Mac，Linux上进行测试。 目前在版本3.6，10，最新 -  1，最新</p>


<div class="section" id="id1">
<h4>Usage<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h4>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Firefox</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:firefox</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="k">my</span> <span class="nv">$driver</span> <span class="o">=</span> <span class="nn">Selenium::Remote::Driver</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">;</span>
</pre></div>
</div>
</div>
<div class="section" id="id2">
<h4>Pros<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>Runs in a real browser and supports JavaScript <span class="subA">运行在真实的浏览器中并支持JavaScript</span> </li>
<li>Faster than the <a class="reference internal" href="#internet-explorer-driver">Internet Explorer Driver</a>
<span class="subA">比Internet Explorer驱动程序更快</span>
</li>
</ul>
</div>
<div class="section" id="id3">
<h4>Cons<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>Slower than the <a class="reference internal" href="#htmlunit-driver">HtmlUnit Driver</a>
<span class="subA">比HtmlUnit驱动程序慢</span>
</li>
</ul>
</div>
<div class="section" id="modifying-the-firefox-profile">
<h4>Modifying the Firefox Profile<a class="headerlink" href="#modifying-the-firefox-profile" title="Permalink to this headline">¶</a></h4>
<p>Suppose that you wanted to modify the user agent string (as above), but you’ve
got a tricked out Firefox profile that contains dozens of useful extensions.
There are two ways to obtain this profile. Assuming that the profile has been
created using Firefox’s profile manager (<code class="docutils literal"><span class="pre">firefox</span> <span class="pre">-ProfileManager</span></code>):</p>
<p>假设您想要修改用户代理字符串（如上所述），但是您已经弄错了包含数十个有用扩展名的Firefox配置文件。 有两种方法可以获得这个配置文件。 假设该配置文件是使用Firefox的配置文件管理器（firefox-Profile文件管理器）创建的：</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">ProfilesIni</span> <span class="n">allProfiles</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ProfilesIni</span><span class="o">();</span>
<span class="n">FirefoxProfile</span> <span class="n">profile</span> <span class="o">=</span> <span class="n">allProfiles</span><span class="o">.</span><span class="na">getProfile</span><span class="o">(</span><span class="s">"WebDriver"</span><span class="o">);</span>
<span class="n">profile</span><span class="o">.</span><span class="na">setPreferences</span><span class="o">(</span><span class="s">"foo.bar"</span><span class="o">,</span> <span class="mi">23</span><span class="o">);</span>
<span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">(</span><span class="n">profile</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="kt">var</span> <span class="n">profileManager</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxProfileManager</span><span class="p">();</span>
<span class="n">FirefoxProfile</span> <span class="n">profile</span> <span class="p">=</span> <span class="n">profileManager</span><span class="p">.</span><span class="n">GetProfile</span><span class="p">(</span><span class="s">"WebDriver"</span><span class="p">);</span>
<span class="n">profile</span><span class="p">.</span><span class="n">SetPreference</span><span class="p">(</span><span class="s">"foo.bar"</span><span class="p">,</span> <span class="m">23</span><span class="p">);</span>
<span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="p">(</span><span class="n">profile</span><span class="p">);</span>
</pre></div>
</div>
<p>Alternatively, if the profile isn’t already registered with Firefox:</p>
<p>或者，如果配置文件尚未在Firefox中注册：</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">File</span> <span class="n">profileDir</span> <span class="o">=</span> <span class="k">new</span> <span class="n">File</span><span class="o">(</span><span class="s">"path/to/top/level/of/profile"</span><span class="o">);</span>
<span class="n">FirefoxProfile</span> <span class="n">profile</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxProfile</span><span class="o">(</span><span class="n">profileDir</span><span class="o">);</span>
<span class="n">profile</span><span class="o">.</span><span class="na">addAdditionalPreferences</span><span class="o">(</span><span class="n">extraPrefs</span><span class="o">);</span>
<span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">(</span><span class="n">profile</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">FirefoxProfile</span> <span class="n">profile</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxProfile</span><span class="p">(</span><span class="s">"path/to/top/level/of/profile"</span><span class="p">);</span>
<span class="n">profile</span><span class="p">.</span><span class="n">SetPreference</span><span class="p">(</span><span class="s">"extra"</span><span class="p">,</span> <span class="s">"pref"</span><span class="p">);</span>
<span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="p">(</span><span class="n">profile</span><span class="p">);</span>
</pre></div>
</div>
<p>As we develop features in the <a class="reference internal" href="#firefox-driver">Firefox Driver</a>, we expose the ability to use them.
For example, until we feel native events are stable on Firefox for Linux, they
are disabled by default. To enable them:</p>
<p>当我们在Firefox驱动程序中开发功能时，我们公开了使用它们的能力。 例如，直到我们感觉Firefox的本地事件在Linux上是稳定的，它们默认是禁用的。 要启用它们：</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">FirefoxProfile</span> <span class="n">profile</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxProfile</span><span class="o">();</span>
<span class="n">profile</span><span class="o">.</span><span class="na">setEnableNativeEvents</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
<span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">(</span><span class="n">profile</span><span class="o">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="kt">var</span> <span class="n">profile</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxProfile</span> <span class="p">{</span><span class="n">EnableNativeEvents</span> <span class="p">=</span> <span class="k">true</span><span class="p">};</span>
<span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="p">(</span><span class="n">profile</span><span class="p">);</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">profile</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">FirefoxProfile</span><span class="p">()</span>
<span class="n">profile</span><span class="o">.</span><span class="n">native_events_enabled</span> <span class="o">=</span> <span class="bp">True</span>
<span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Firefox</span><span class="p">(</span><span class="n">profile</span><span class="p">)</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">profile</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Firefox</span><span class="o">::</span><span class="no">Profile</span><span class="o">.</span><span class="n">new</span>
<span class="n">profile</span><span class="o">.</span><span class="n">native_events</span> <span class="o">=</span> <span class="kp">true</span>
<span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:firefox</span><span class="p">,</span> <span class="ss">:profile</span> <span class="o">=&gt;</span> <span class="n">profile</span>
</pre></div>
</div>
</div>
<div class="section" id="info">
<h4>Info<a class="headerlink" href="#info" title="Permalink to this headline">¶</a></h4>
<p>See the <a class="reference external" href="https://github.com/SeleniumHQ/selenium/wiki/FirefoxDriver">Firefox section in the wiki page</a> for the most up to date info.</p>
<p>请参阅Wiki页面中的Firefox部分以获取最新的信息。</p>

</div>
</div>
<div class="section" id="internet-explorer-driver">
<h3>Internet Explorer Driver<a class="headerlink" href="#internet-explorer-driver" title="Permalink to this headline">¶</a></h3>
<p>The InternetExplorerDriver is a standalone server which implements WebDriver’s wire protocol. This driver has been tested with IE 7, 8, 9, 10, and 11 on appropriate combinations of Vista, Windows 7, Windows 8, and Windows 8.1. As of 15 April 2014, IE 6 is no longer supported.</p>
<p>InternetExplorerDriver是一个独立的服务器，实现WebDriver的有线协议。 该驱动程序已经在Vista，Windows 7，Windows 8和Windows 8.1的适当组合上测试了IE 7,8,9,10和11。 截至2014年4月15日，IE 6不再受支持。</p>

<p>The driver supports running 32-bit and 64-bit versions of the browser. The choice of how to determine which “bit-ness” to use in launching the browser depends on which version of the IEDriverServer.exe is launched. If the 32-bit version of IEDriverServer.exe is launched, the 32-bit version of IE will be launched. Similarly, if the 64-bit version of IEDriverServer.exe is launched, the 64-bit version of IE will be launched.</p>
<p>该驱动程序支持运行32位和64位版本的浏览器。 如何确定在启动浏览器时使用哪个“bit-ness”取决于启动哪个版本的IEDriverServer.exe。 如果启动32位版本的IEDriverServer.exe，将启动32位版本的IE。 同样，如果启动64位版本的IEDriverServer.exe，则将启动64位版本的IE。</p>

<div class="section" id="id4">
<h4>Usage<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h4>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">InternetExplorerDriver</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">InternetExlorerDriver</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Ie</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:ie</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="k">my</span> <span class="nv">$driver</span> <span class="o">=</span> <span class="nn">Selenium::Remote::Driver</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">(</span><span class="n">browser_name</span> <span class="o">=&gt;</span> <span class="s">'internet explorer'</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="id5">
<h4>Pros<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>Runs in a real browser and supports Javascript <span class="subA">在真实浏览器中运行并支持Javascript</span> </li>
</ul>
</div>
<div class="section" id="id6">
<h4>Cons<a class="headerlink" href="#id6" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>Obviously the InternetExplorerDriver will only work on Windows! <span class="subA">InternetExplorerDriver显然只能在Windows上运行！</span> </li>
<li>Comparatively slow (though still pretty snappy!) <span class="subA">比较慢（虽然仍然非常活泼！）</span> </li>
</ul>
</div>
<div class="section" id="id7">
<h4>Info<a class="headerlink" href="#id7" title="Permalink to this headline">¶</a></h4>
<p>See the <a class="reference external" href="https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver">Internet Explorer section of the wiki page</a> for the most up to date info.
Please take special note of the Required Configuration section.</p>
<p>请参阅wiki页面的Internet Explorer部分以获取最新的信息。 请特别注意所需的配置部分。</p>

</div>
</div>
<div class="section" id="chromedriver">
<h3>ChromeDriver<a class="headerlink" href="#chromedriver" title="Permalink to this headline">¶</a></h3>
<p>ChromeDriver is maintained / supported by the <a class="reference external" href="http://code.google.com/p/chromium/">Chromium</a>
project iteslf. WebDriver works with Chrome through the chromedriver binary (found on the chromium
project’s download page). You need to have both chromedriver and a version of chrome browser installed.
chromedriver needs to be placed somewhere on your system’s path in order for WebDriver to automatically
discover it. The Chrome browser itself is discovered by chromedriver in the default installation path.
These both can be overridden by environment variables. Please refer to <a class="reference external" href="https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver">the wiki</a>
for more information.</p>
<p>ChromeDriver由Chromium项目维护/支持。 WebDriver通过chromedriver二进制文件（可在铬项目的下载页面找到）与Chrome一起使用。 你需要同时安装chromedriver和chrome浏览器。 需要将chromedriver放置在系统路径的某个位置，以便WebDriver自动发现它。 Chrome浏览器本身是由默认安装路径中的chromedriver发现的。 这两者都可以被环境变量覆盖。 请参阅wiki以获取更多信息。</p>

<div class="section" id="id8">
<h4>Usage<a class="headerlink" href="#id8" title="Permalink to this headline">¶</a></h4>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ChromeDriver</span><span class="o">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-csharp" value="csharp" src="images/icons/csharp.png" style="padding: 3px;"><div class="highlight-csharp" style="display: none;"><div class="highlight"><pre><span></span><span class="n">IWebDriver</span> <span class="n">driver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ChromeDriver</span><span class="p">();</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-python" value="python" src="images/icons/python.png" style="padding: 3px;"><div class="highlight-python" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Chrome</span><span class="p">()</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-ruby" value="ruby" src="images/icons/ruby.png" style="padding: 3px;"><div class="highlight-ruby" style="display: none;"><div class="highlight"><pre><span></span><span class="n">driver</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">.</span><span class="n">for</span> <span class="ss">:chrome</span>
</pre></div>
</div>
<input type="image" class="toggler toggler-perl" value="perl" src="images/icons/perl.png" style="padding: 3px;"><div class="highlight-perl" style="display: none;"><div class="highlight"><pre><span></span><span class="k">my</span> <span class="nv">$driver</span> <span class="o">=</span> <span class="nn">Selenium::Remote::Driver</span><span class="o">-&gt;</span><span class="k">new</span><span class="p">(</span><span class="n">browser_name</span> <span class="o">=&gt;</span> <span class="s">'chrome'</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="id9">
<h4>Pros<a class="headerlink" href="#id9" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>Runs in a real browser and supports JavaScript</li>
<li>Because Chrome is a Webkit-based browser, the <a class="reference internal" href="#chromedriver">ChromeDriver</a> may allow you to
verify that your site works in Safari. Note that since Chrome uses its own V8
JavaScript engine rather than Safari’s Nitro engine, JavaScript execution may
differ. <span class="subA">由于Chrome是基于Webkit的浏览器，ChromeDriver可能允许您验证您的网站是否在Safari中运行。 请注意，由于Chrome浏览器使用自己的V8 JavaScript引擎而不是Safari的Nitro引擎，因此JavaScript执行可能会有所不同。</span> </li>
</ul>
</div>
<div class="section" id="id10">
<h4>Cons<a class="headerlink" href="#id10" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>Slower than the <a class="reference internal" href="#htmlunit-driver">HtmlUnit Driver</a></li>
</ul>
</div>
<div class="section" id="id11">
<h4>Info<a class="headerlink" href="#id11" title="Permalink to this headline">¶</a></h4>
<p><a class="reference external" href="https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver">See our wiki</a> for the most up to date info.
More info can also be found on the <a class="reference external" href="/download/">downloads page</a></p>
</div>
<div class="section" id="getting-running-with-chromedriver">
<h4>Getting running with ChromeDriver<a class="headerlink" href="#getting-running-with-chromedriver" title="Permalink to this headline">¶</a></h4>
<p>Download the <a class="reference external" href="https://sites.google.com/a/chromium.org/chromedriver/">ChromeDriver executable</a>
and follow the other instructions on the
<a class="reference external" href="https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver">wiki page</a></p>
</div>
</div>
<div class="section" id="opera-driver">
<span id="seleniumrcemulation"></span><h3>Opera Driver<a class="headerlink" href="#opera-driver" title="Permalink to this headline">¶</a></h3>
<p>See the <a class="reference external" href="https://github.com/SeleniumHQ/selenium/wiki/OperaDriver">Opera Driver wiki article</a> in the
Selenium Wiki for information on using the Opera Driver.</p>
</div>
<div class="section" id="ios-driver">
<h3>iOS Driver<a class="headerlink" href="#ios-driver" title="Permalink to this headline">¶</a></h3>
<p>See either the <a class="reference external" href="http://ios-driver.github.io/ios-driver/">ios-driver</a> or <a class="reference external" href="http://appium.io">appium</a> projects.</p>
</div>
<div class="section" id="android-driver">
<h3>Android Driver<a class="headerlink" href="#android-driver" title="Permalink to this headline">¶</a></h3>
<p>See the <a class="reference external" href="http://selendroid.io">Selendroid project</a></p>
</div>
</div>
<div class="section" id="alternative-back-ends-mixing-webdriver-and-rc-technologies">
<h2>Alternative Back-Ends: Mixing WebDriver and RC Technologies<a class="headerlink" href="#alternative-back-ends-mixing-webdriver-and-rc-technologies" title="Permalink to this headline">¶</a></h2>
<div class="section" id="webdriver-backed-selenium-rc">
<h3>WebDriver-Backed Selenium-RC<a class="headerlink" href="#webdriver-backed-selenium-rc" title="Permalink to this headline">¶</a></h3>
<p>The Java version of WebDriver provides an implementation of the Selenium-RC API.  These means that
you can use the underlying WebDriver technology using the Selenium-RC API.  This is primarily
provided for backwards compatibility.  It allows those who have existing test suites using the
Selenium-RC API to use WebDriver under the covers.  It’s provided to help ease the migration path
to Selenium-WebDriver.  Also, this allows one to use both APIs, side-by-side, in the same test code.</p>
<p>WebDriver的Java版本提供了Selenium-RC API的实现。 这意味着您可以使用基础WebDriver技术使用Selenium-RC API。 这主要是为了向后兼容。 它允许那些使用Selenium-RC API的现有测试套件使用WebDriver。 它提供了帮助缓解Selenium-WebDriver的迁移路径。 此外，这允许在相同的测试代码中并行使用两个API。</p>

<p>Selenium-WebDriver is used like this:</p>
<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="c1">// You may use any WebDriver implementation. Firefox is used here as an example</span>
<span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FirefoxDriver</span><span class="o">();</span>

<span class="c1">// A "base url", used by selenium to resolve relative URLs</span>
 <span class="n">String</span> <span class="n">baseUrl</span> <span class="o">=</span> <span class="s">"http://www.google.com"</span><span class="o">;</span>

<span class="c1">// Create the Selenium implementation</span>
<span class="n">Selenium</span> <span class="n">selenium</span> <span class="o">=</span> <span class="k">new</span> <span class="n">WebDriverBackedSelenium</span><span class="o">(</span><span class="n">driver</span><span class="o">,</span> <span class="n">baseUrl</span><span class="o">);</span>

<span class="c1">// Perform actions with selenium</span>

<span class="n">selenium</span><span class="o">.</span><span class="na">open</span><span class="o">(</span><span class="s">"http://www.google.com"</span><span class="o">);</span>
<span class="n">selenium</span><span class="o">.</span><span class="na">type</span><span class="o">(</span><span class="s">"name=q"</span><span class="o">,</span> <span class="s">"cheese"</span><span class="o">);</span>
<span class="n">selenium</span><span class="o">.</span><span class="na">click</span><span class="o">(</span><span class="s">"name=btnG"</span><span class="o">);</span>

<span class="c1">// Get the underlying WebDriver implementation back. This will refer to the</span>
<span class="c1">// same WebDriver instance as the "driver" variable above.</span>
<span class="n">WebDriver</span> <span class="n">driverInstance</span> <span class="o">=</span> <span class="o">((</span><span class="n">WebDriverBackedSelenium</span><span class="o">)</span> <span class="n">selenium</span><span class="o">).</span><span class="na">getWrappedDriver</span><span class="o">();</span>

<span class="c1">//Finally, close the browser. Call stop on the WebDriverBackedSelenium instance</span>
<span class="c1">//instead of calling driver.quit(). Otherwise, the JVM will continue running after</span>
<span class="c1">//the browser has been closed.</span>
<span class="n">selenium</span><span class="o">.</span><span class="na">stop</span><span class="o">();</span>
</pre></div>
</div>
<div class="section" id="id13">
<h4>Pros<a class="headerlink" href="#id13" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>Allows for the WebDriver and Selenium APIs to live side-by-side <span class="subA">允许WebDriver和Selenium API并行生存</span></li>
<li>Provides a simple mechanism for a managed migration from the Selenium RC API
to WebDriver’s <span class="subA">为从Selenium RC API到WebDriver的受管迁移提供了一个简单的机制</span></li>
<li>Does not require the standalone Selenium RC server to be run <span class="subA">不需要独立的Selenium RC服务器运行</span></li>
</ul>
</div>
<div class="section" id="id14">
<h4>Cons<a class="headerlink" href="#id14" title="Permalink to this headline">¶</a></h4>
<ul class="simple">
<li>Does not implement every method <span class="subA">不执行每个方法</span></li>
<li>More advanced Selenium usage (using “browserbot” or other built-in JavaScript
methods from Selenium Core) may not work <span class="subA">更高级的Selenium使用（使用“browserbot”或Selenium Core中的其他内置JavaScript方法）可能无法正常工作</span></li>
<li>Some methods may be slower due to underlying implementation differences <span class="subA">由于潜在的实施差异，一些方法可能会变慢</span></li>
</ul>
</div>
</div>
<div class="section" id="backing-webdriver-with-selenium">
<h3>Backing WebDriver with Selenium<a class="headerlink" href="#backing-webdriver-with-selenium" title="Permalink to this headline">¶</a></h3>
<p>WebDriver doesn’t support as many browsers as Selenium RC does, so in order to
provide that support while still using the WebDriver API, you can make use of
the <code class="docutils literal"><span class="pre">SeleneseCommandExecutor</span></code></p>
<p>WebDriver不像Selenium RC那样支持很多浏览器，所以为了在仍然使用WebDriver API的情况下提供这种支持，你可以使用SeleneseCommandExecutor</p>

<p>Safari is supported in this way with the following code (be sure to disable
pop-up blocking):</p>
<p>Safari以这种方式支持以下代码（确保禁用弹出窗口阻止）：</p>

<input type="image" class="toggler toggler-java" value="java" src="images/icons/highlight-java.png" style="padding: 3px;"><div class="highlight-java"><div class="highlight"><pre><span></span><span class="n">DesiredCapabilities</span> <span class="n">capabilities</span> <span class="o">=</span> <span class="k">new</span> <span class="n">DesiredCapabilities</span><span class="o">();</span>
<span class="n">capabilities</span><span class="o">.</span><span class="na">setBrowserName</span><span class="o">(</span><span class="s">"safari"</span><span class="o">);</span>
<span class="n">CommandExecutor</span> <span class="n">executor</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SeleneseCommandExecutor</span><span class="o">(</span><span class="k">new</span> <span class="n">URL</span><span class="o">(</span><span class="s">"http://localhost:4444/"</span><span class="o">),</span> <span class="k">new</span> <span class="n">URL</span><span class="o">(</span><span class="s">"http://www.google.com/"</span><span class="o">),</span> <span class="n">capabilities</span><span class="o">);</span>
<span class="n">WebDriver</span> <span class="n">driver</span> <span class="o">=</span> <span class="k">new</span> <span class="n">RemoteWebDriver</span><span class="o">(</span><span class="n">executor</span><span class="o">,</span> <span class="n">capabilities</span><span class="o">);</span>
</pre></div>
</div>
<p>There are currently some major limitations with this approach, notably that
findElements doesn’t work as expected. Also, because we’re using Selenium Core
for the heavy lifting of driving the browser, you are limited by the JavaScript
sandbox.</p>
<p>这种方法目前有一些主要的限制，特别是findElements不能按预期工作。 另外，因为我们正在使用Selenium Core来驱动浏览器，所以受到JavaScript沙箱的限制。</p>

</div>
</div>
<div class="section" id="running-standalone-selenium-server-for-use-with-remotedrivers">
<h2>Running Standalone Selenium Server for use with RemoteDrivers<a class="headerlink" href="#running-standalone-selenium-server-for-use-with-remotedrivers" title="Permalink to this headline">¶</a></h2>
<p>From <a class="reference external" href="http://selenium-release.storage.googleapis.com/index.html">Selenium’s Download page</a> download selenium-server-standalone-&lt;version&gt;.jar and optionally IEDriverServer. If you plan to work with Chrome, download it from <a class="reference external" href="http://chromedriver.googlecode.com/">Google Code</a>.</p>
<p>从Selenium的下载页面下载selenium-server-standalone- &lt;version&gt; .jar和可选的IEDriverServer。 如果您计划使用Chrome，请从Google代码下载。</p>

<p>Unpack IEDriverServer and/or chromedriver and put them in a directory which is on the $PATH / %PATH% - the Selenium Server should then be able to handle requests for IE / Chrome without additional modifications.</p>
<p>解压IEDriverServer和/或chromedriver，并把它们放在$ PATH /％PATH％的目录中 - 然后Selenium服务器就可以处理IE / Chrome的请求而无需额外的修改。</p>

<p>Start the server on the command line with</p>
<p>用命令行启动服务器</p>

<div class="highlight-bash"><div class="highlight"><pre><span></span>java -jar &lt;path_to&gt;/selenium-server-standalone-&lt;version&gt;.jar
</pre></div>
</div>
<p>If you want to use native events functionality, indicate this on the command line with the option</p>
<p>如果您想使用本机事件功能，请在命令行上使用该选项指明</p>

<div class="highlight-bash"><div class="highlight"><pre><span></span>-Dwebdriver.enable.native.events<span class="o">=</span><span class="m">1</span>
</pre></div>
</div>
<p>For other command line options, execute</p>
<p>对于其他命令行选项，执行</p>

<div class="highlight-bash"><div class="highlight"><pre><span></span>java -jar &lt;path_to&gt;/selenium-server-standalone-&lt;version&gt;.jar -help
</pre></div>
</div>
<p>In order to function properly, the following ports should be allowed incoming TCP connections: 4444, 7054-5 (or twice as many ports as the number of concurrent instances you plan to run). Under Windows, you may need to unblock the applications as well.</p>
<p>为了正常工作，应该允许以下端口进入TCP连接：4444，7054-5（或者计划运行的并发实例数量的两倍）。 在Windows下，您也可能需要解锁应用程序。</p>

</div>
<div class="section" id="additional-resources">
<span id="seleniumwebdriverwiki"></span><h2>Additional Resources<a class="headerlink" href="#additional-resources" title="Permalink to this headline">¶</a></h2>
<p>You can find further resources for WebDriver
in <a class="reference external" href="https://github.com/SeleniumHQ/selenium/wiki/FurtherResources">WebDriver’s wiki</a></p>
<p>您可以在WebDriver的wiki中找到WebDriver的更多资源</p>

<p>Of course, don’t hesitate to do an internet search on any Selenium topic, including
Selenium-WebDriver’s drivers.  There are quite a few blogs on Selenium along with numerous posts
on various user forums.  Additionally the Selenium User’s Group is a great resource.
<a class="reference external" href="http://groups.google.com/group/selenium-users">http://groups.google.com/group/selenium-users</a></p>
<p>当然，不要犹豫在Selenium主题上进行网络搜索，包括Selenium-WebDriver的驱动程序。 Selenium上有不少博客，还有各种用户论坛上的大量帖子。 此外，硒用户组是一个很好的资源。http://groups.google.com/group/selenium-users</p>

</div>
<div class="section" id="next-steps">
<h2>Next Steps<a class="headerlink" href="#next-steps" title="Permalink to this headline">¶</a></h2>
<p>This chapter has simply been a high level walkthrough of WebDriver and some of its key
capabilities.  Once getting familiar with the Selenium-WebDriver API you will then want to learn
how to build test suites for maintainability, extensibility, and reduced fragility when features of
the AUT frequently change.  The approach most Selenium experts are now recommending is to design
your test code using the Page Object Design Pattern along with possibly a Page Factory.
Selenium-WebDriver provides support for this by supplying a PageFactory class in Java and C#.
This is presented, along with other advanced topics, in the
<a class="reference internal" href="04_webdriver_advanced.jsp#chapter04-reference"><span class="std std-ref">next chapter</span></a>.  Also, for high-level description of this
technique, you may want to look at the
<a class="reference internal" href="06_test_design_considerations.jsp#chapter06-reference"><span class="std std-ref">Test Design Considerations chapter</span></a>.  Both of these
chapters present techniques for writing more maintainable tests by making your test code more
modular.</p>
<p>本章简单介绍了WebDriver及其一些关键功能的高级演练。 一旦熟悉了Selenium-WebDriver API，您就会想要学习如何构建测试套件以实现可维护性，可扩展性以及在AUT的功能频繁变化时降低易碎性。 现在大多数Selenium专家都建议使用页面对象设计模式和页面工厂来设计测试代码。 Selenium-WebDriver通过提供Java和C＃中的PageFactory类来提供对此的支持。 这是在下一章中介绍的，以及其他高级主题。 此外，对于这种技术的高级描述，您可能需要查看“测试设计注意事项”一章。 这两章都介绍了通过使测试代码更加模块化来编写更多可维护测试的技术。</p>

</div>
</div>


          </div>


</body>
</html>